Miss Islington (bot) | e299130 | 2020-05-19 03:03:25 -0700 | [diff] [blame] | 1 | """High-level support for working with threads in asyncio""" |
| 2 | |
| 3 | import functools |
Miss Islington (bot) | 3e65054 | 2020-05-20 22:38:00 -0700 | [diff] [blame] | 4 | import contextvars |
Miss Islington (bot) | e299130 | 2020-05-19 03:03:25 -0700 | [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 |
Miss Islington (bot) | 3e65054 | 2020-05-20 22:38:00 -0700 | [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. |
Miss Islington (bot) | e299130 | 2020-05-19 03:03:25 -0700 | [diff] [blame] | 19 | |
Miss Islington (bot) | cdb015b | 2020-05-31 00:26:20 -0700 | [diff] [blame] | 20 | Return a coroutine that can be awaited to get the eventual result of *func*. |
Miss Islington (bot) | e299130 | 2020-05-19 03:03:25 -0700 | [diff] [blame] | 21 | """ |
| 22 | loop = events.get_running_loop() |
Miss Islington (bot) | 3e65054 | 2020-05-20 22:38:00 -0700 | [diff] [blame] | 23 | ctx = contextvars.copy_context() |
| 24 | func_call = functools.partial(ctx.run, func, *args, **kwargs) |
Miss Islington (bot) | e299130 | 2020-05-19 03:03:25 -0700 | [diff] [blame] | 25 | return await loop.run_in_executor(None, func_call) |