From fbc22f81ce2eb360609f8570eecfa25bf00d9036 Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Thu, 21 May 2020 01:20:43 -0400 Subject: [PATCH] bpo-32309: Add support for contextvars in asyncio.to_thread() (GH-20278) Allows contextvars from the main thread to be accessed in the separate thread used in `asyncio.to_thread()`. See the [discussion](https://github.com/python/cpython/pull/20143#discussion_r427808225) in GH-20143 for context. Automerge-Triggered-By: @aeros --- Doc/library/asyncio-task.rst | 6 +++++- Lib/asyncio/threads.py | 8 ++++++-- Lib/test/test_asyncio/test_threads.py | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index 7c2704090551b6..dd94c14854835b 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -610,7 +610,9 @@ Running in Threads Asynchronously run function *func* in a separate thread. Any \*args and \*\*kwargs supplied for this function are directly passed - to *func*. + to *func*. Also, the current :class:`contextvars.Context` is propogated, + allowing context variables from the event loop thread to be accessed in the + separate thread. Return an :class:`asyncio.Future` which represents the eventual result of *func*. @@ -657,6 +659,8 @@ Running in Threads that release the GIL or alternative Python implementations that don't have one, `asyncio.to_thread()` can also be used for CPU-bound functions. + .. versionadded:: 3.9 + Scheduling From Other Threads ============================= diff --git a/Lib/asyncio/threads.py b/Lib/asyncio/threads.py index 2f40467fe5bc7b..51e0ba95d822e5 100644 --- a/Lib/asyncio/threads.py +++ b/Lib/asyncio/threads.py @@ -1,6 +1,7 @@ """High-level support for working with threads in asyncio""" import functools +import contextvars from . import events @@ -12,10 +13,13 @@ async def to_thread(func, /, *args, **kwargs): """Asynchronously run function *func* in a separate thread. Any *args and **kwargs supplied for this function are directly passed - to *func*. + to *func*. Also, the current :class:`contextvars.Context` is propogated, + allowing context variables from the main thread to be accessed in the + separate thread. Return an asyncio.Future which represents the eventual result of *func*. """ loop = events.get_running_loop() - func_call = functools.partial(func, *args, **kwargs) + ctx = contextvars.copy_context() + func_call = functools.partial(ctx.run, func, *args, **kwargs) return await loop.run_in_executor(None, func_call) diff --git a/Lib/test/test_asyncio/test_threads.py b/Lib/test/test_asyncio/test_threads.py index 99a00f21832f3e..2af322421dacfa 100644 --- a/Lib/test/test_asyncio/test_threads.py +++ b/Lib/test/test_asyncio/test_threads.py @@ -3,6 +3,7 @@ import asyncio import unittest +from contextvars import ContextVar from unittest import mock from test.test_asyncio import utils as test_utils @@ -74,6 +75,19 @@ async def main(): self.loop.run_until_complete(main()) func.assert_called_once_with('test', something=True) + def test_to_thread_contextvars(self): + test_ctx = ContextVar('test_ctx') + + def get_ctx(): + return test_ctx.get() + + async def main(): + test_ctx.set('parrot') + return await asyncio.to_thread(get_ctx) + + result = self.loop.run_until_complete(main()) + self.assertEqual(result, 'parrot') + if __name__ == "__main__": unittest.main()