blob: 2f40467fe5bc7ba48937d33267d6152d631533ca [file] [log] [blame]
Miss Islington (bot)e2991302020-05-19 03:03:25 -07001"""High-level support for working with threads in asyncio"""
2
3import functools
4
5from . import events
6
7
8__all__ = "to_thread",
9
10
11async 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)