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 |
| 4 | |
| 5 | from . import events |
| 6 | |
| 7 | |
| 8 | __all__ = "to_thread", |
| 9 | |
| 10 | |
| 11 | async def to_thread(func, /, *args, **kwargs): |
| 12 | """Asynchronously run function *func* in a separate thread. |
| 13 | |
| 14 | Any *args and **kwargs supplied for this function are directly passed |
| 15 | to *func*. |
| 16 | |
| 17 | Return an asyncio.Future which represents the eventual result of *func*. |
| 18 | """ |
| 19 | loop = events.get_running_loop() |
| 20 | func_call = functools.partial(func, *args, **kwargs) |
| 21 | return await loop.run_in_executor(None, func_call) |