-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9754a5
commit 0992d57
Showing
1 changed file
with
16 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
import asyncio | ||
from typing import Callable, ParamSpec, TypeVar | ||
import sys | ||
from typing import Any, Callable, TypeVar | ||
|
||
T = TypeVar('T') | ||
P = ParamSpec('P') | ||
|
||
if sys.version_info >= (3, 10): | ||
from typing import ParamSpec | ||
P = ParamSpec('P') | ||
|
||
async def io_bound(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T: | ||
"""Run a blocking function in a thread pool executor. | ||
This is useful for disk I/O operations that would block the event loop.""" | ||
loop = asyncio.get_event_loop() | ||
return await loop.run_in_executor(None, lambda: func(*args, **kwargs)) | ||
async def io_bound(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T: | ||
"""Run a blocking function in a thread pool executor. | ||
This is useful for disk I/O operations that would block the event loop.""" | ||
loop = asyncio.get_event_loop() | ||
return await loop.run_in_executor(None, lambda: func(*args, **kwargs)) | ||
else: | ||
async def io_bound(func: Callable[..., T], *args: Any, **kwargs: Any) -> T: | ||
"""Run a blocking function in a thread pool executor. | ||
This is useful for disk I/O operations that would block the event loop.""" | ||
loop = asyncio.get_event_loop() | ||
return await loop.run_in_executor(None, lambda: func(*args, **kwargs)) |