Kyle Stanley | cc2bbc2 | 2020-05-18 23:03:28 -0400 | [diff] [blame] | 1 | """High-level support for working with threads in asyncio""" |
| 2 | |
| 3 | import functools |
Kyle Stanley | 0f56263 | 2020-05-21 01:20:43 -0400 | [diff] [blame] | 4 | import contextvars |
Kyle Stanley | cc2bbc2 | 2020-05-18 23:03:28 -0400 | [diff] [blame] | 5 | |
| 6 | from . import events |
| 7 | |
| 8 | |
| 9 | __all__ = "to_thread", |
| 10 | |
| 11 | |
| 12 | async def to_thread(func, /, *args, **kwargs): |
| 13 | """Asynchronously run function *func* in a separate thread. |
| 14 | |
| 15 | Any *args and **kwargs supplied for this function are directly passed |
Kyle Stanley | 0f56263 | 2020-05-21 01:20:43 -0400 | [diff] [blame] | 16 | to *func*. Also, the current :class:`contextvars.Context` is propogated, |
| 17 | allowing context variables from the main thread to be accessed in the |
| 18 | separate thread. |
Kyle Stanley | cc2bbc2 | 2020-05-18 23:03:28 -0400 | [diff] [blame] | 19 | |
Kyle Stanley | 2b20136 | 2020-05-31 03:07:04 -0400 | [diff] [blame] | 20 | Return a coroutine that can be awaited to get the eventual result of *func*. |
Kyle Stanley | cc2bbc2 | 2020-05-18 23:03:28 -0400 | [diff] [blame] | 21 | """ |
| 22 | loop = events.get_running_loop() |
Kyle Stanley | 0f56263 | 2020-05-21 01:20:43 -0400 | [diff] [blame] | 23 | ctx = contextvars.copy_context() |
| 24 | func_call = functools.partial(ctx.run, func, *args, **kwargs) |
Kyle Stanley | cc2bbc2 | 2020-05-18 23:03:28 -0400 | [diff] [blame] | 25 | return await loop.run_in_executor(None, func_call) |