[New-bugs-announce] [issue40320] Add ability to specify instance of contextvars context to Task() & asyncio.create_task()

Jeff Laughlin report at bugs.python.org
Sat Apr 18 11:28:07 EDT 2020


New submission from Jeff Laughlin <jeff.laughlin at gmail.com>:

As a test engineer I want to be able to run async test fixtures and test cases in different async tasks with the same Context. Not a copy; the same specific instance of contextvars.Context().

I do NOT want the task to run in a COPY of the context because I want mutations to the context to be preserved so that I can pass the mutated context into another async task.

I do NOT want the task to inherit the potentially polluted global context.

class Task currently unconditionally copies the current global context and has no facility for the user to override the context.

Therefor I propose adding a context argument to the Task constructor and to create_task()

It should be noted that this argument should not be used for "normal" development and only for "weird" stuff like test frameworks.

I should also note that Context().run() is not useful here because it is not async and there is no obvious existing async equivalent. This proposal would be roughly equivalent.

I should also note that a hack like copying the items from one context to another will not work because it breaks ContextVar set/reset. I tried this. It was a heroic failure. It must be possible to run a task with an exist instance of context and not a copy.

Here is a real-world use case: https://github.com/pytest-dev/pytest-asyncio/pull/153/files

Here is the hacked Task constructor I cooked up:

class Task(asyncio.tasks._PyTask):
    def __init__(self, coro, *, loop=None, name=None, context=None):
...
        self._context = context if context is not None else copy_context()

        self._loop.call_soon(self.__step, context=self._context)
        asyncio._register_task(self) 


if folks are on board I can do a PR

----------
components: asyncio
messages: 366722
nosy: Jeff.Laughlin, asvetlov, yselivanov
priority: normal
severity: normal
status: open
title: Add ability to specify instance of contextvars context to Task() & asyncio.create_task()
type: enhancement
versions: Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40320>
_______________________________________


More information about the New-bugs-announce mailing list