bpo-32309: Add support for contextvars in asyncio.to_thread() (GH-20278) · python/cpython@0f56263 (original) (raw)

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1 1 """High-level support for working with threads in asyncio"""
2 2
3 3 import functools
4 +import contextvars
4 5
5 6 from . import events
6 7
@@ -12,10 +13,13 @@ async def to_thread(func, /, *args, **kwargs):
12 13 """Asynchronously run function *func* in a separate thread.
13 14
14 15 Any *args and **kwargs supplied for this function are directly passed
15 - to *func*.
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.
16 19
17 20 Return an asyncio.Future which represents the eventual result of *func*.
18 21 """
19 22 loop = events.get_running_loop()
20 -func_call = functools.partial(func, *args, **kwargs)
23 +ctx = contextvars.copy_context()
24 +func_call = functools.partial(ctx.run, func, *args, **kwargs)
21 25 return await loop.run_in_executor(None, func_call)