-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added call of synchronous functions using ThreadPoolExecutor in creat…
…e_snapshot to avoid blocking the loop
- Loading branch information
Showing
2 changed files
with
22 additions
and
3 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
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import asyncio | ||
import contextvars | ||
import functools | ||
from typing import Callable | ||
from typing import TypeVar | ||
|
||
R = TypeVar("R") | ||
|
||
|
||
async def run_in_thread_poll_executor(f: Callable[..., R], *args, **kwargs) -> R: | ||
loop = asyncio.get_running_loop() | ||
ctx = contextvars.copy_context() | ||
f_with_args = functools.partial(f, *args, **kwargs) | ||
return await loop.run_in_executor(None, lambda: ctx.run(f_with_args)) |