With normal synchronous code you can use `contextvars.Context.run()` to change 
what context code is executing within. However, there is no analagous concept 
for asyncio code. I'm proposing something similar, for example:

coro = foo()
my_context = convextvars.Context()
await asyncio.run_in_context(coro)

Currently the workaround is to run the coroutine on a separate task.

coro = foo()
my_context = convextvars.Context()
await my_context.run(asycnio.create_task, coro)

However this is not exactlly the same as the task will inherit a copy of 
my_context rather than running directly on my_context. Additionally (obviously) 
it will also be running in a separate task.

Similarly it would be nice if create_task and the Task constructor could take 
an optional context kwarg to use as the task context rather than the default of 
copying the calling context.

Pull request with sample implementation (although I think missing the change to 
create_task): https://github.com/python/cpython/pull/26664
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/3EQO67J7IBAO6YSK2LBIPU4E7HU6UQJH/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to