> However this is not exactlly the same as the task will inherit a copy of
my_context rather than running directly on my_context.

Yeah, it would indeed inherit the copy. We could, theoretically, make
asyncio.Task accept context objects and not copy them, but what would that
give us? If a coroutine awaits on some code that calls `copy_context()`
internally you will not be able to observe the modifications that code
makes to its forked context. *Ultimately, contextvars enable implicit flow
of information from outer code to nested code and not vice versa. *

> Additionally (obviously) it will also be running in a separate task.

There's no way around that, unfortunately. Even if we add some kind of
helper to run coroutines in a context, there still we be a task object that
iterates the coroutine.

> 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.

I guess we can add a keyword argument to asyncio.create_task() for that. It
is an open question if the task factory would just use the passed context
object or would copy it first. I'm leaning towards the latter.

Yury



On Sun, Jun 20, 2021 at 3:13 PM Mark Gordon <[email protected]> wrote:

> 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/
>


-- 
         Yury
_______________________________________________
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/J7NOEWBUSB5TLHD2ISB5HC3GB5E6AAVQ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to