New submission from Jeff Laughlin :
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
<https://bugs.python.org/issue40320>
___
___
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com