[Python-ideas] PEP draft: context variables

Yury Selivanov yselivanov.ml at gmail.com
Fri Oct 13 12:41:47 EDT 2017


On Fri, Oct 13, 2017 at 3:25 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
[..]
> However, considering that coroutines are almost always instantiated at the
> point where they're awaited, I do concede that creation time context capture
> would likely also work out OK for the coroutine case, which would leave
> contextlib.contextmanager as the only special case (and it would turn off
> both creation-time context capture *and* context isolation).

Actually, capturing context at the moment of coroutine creation (in
PEP 550 v1 semantics) will not work at all.  Async context managers
will break.

   class AC:
       async def __aenter__(self):
            pass

^ If the context is captured when coroutines are instantiated,
__aenter__ won't be able to set context variables and thus affect the
code it wraps.  That's why coroutines shouldn't capture context when
created, nor they should isolate context.  It's a job of async Task.

Yury


More information about the Python-ideas mailing list