Skip to content

bpo-47062: Implement asyncio.Runner context manager #31799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
61f10d0
Implement Runner class
asvetlov Mar 9, 2022
d471799
Add get_loop() / new_loop() methods
asvetlov Mar 10, 2022
c68ba85
Clarify
asvetlov Mar 10, 2022
d530481
Add tests
asvetlov Mar 10, 2022
6ee7c9a
Add a comment
asvetlov Mar 10, 2022
e15d70b
Adopt IsolatedAsyncioTestCase to use Runner
asvetlov Mar 10, 2022
5c7a669
Merge branch 'main' into asyncio-runner
asvetlov Mar 10, 2022
d41988d
Merge branch 'main' into asyncio-runner
asvetlov Mar 10, 2022
8014227
Add docs sketch
asvetlov Mar 12, 2022
98e39db
Merge branch 'main' into asyncio-runner
asvetlov Mar 14, 2022
98e7a22
Add context arg to Runner.run()
asvetlov Mar 14, 2022
f4cd673
Work on docs
asvetlov Mar 17, 2022
1764045
Merge branch 'main' into asyncio-runner
asvetlov Mar 18, 2022
a443b37
Work on
asvetlov Mar 18, 2022
e6be8f7
Improve docs
asvetlov Mar 18, 2022
b1dfe4f
Add NEWS
asvetlov Mar 18, 2022
759f72a
Drop not related file
asvetlov Mar 18, 2022
f47d66a
Fix doc
asvetlov Mar 18, 2022
546440b
Update Lib/asyncio/runners.py
asvetlov Mar 18, 2022
6935f7d
Update Doc/library/asyncio-runner.rst
asvetlov Mar 18, 2022
9b9a004
Update Lib/asyncio/runners.py
asvetlov Mar 18, 2022
b0c5b8c
Improve wording
asvetlov Mar 19, 2022
599c9db
Add a test for double 'with' usage
asvetlov Mar 19, 2022
b0da74b
Improve tests
asvetlov Mar 19, 2022
04cfff9
Work on
asvetlov Mar 22, 2022
8753465
Merge branch 'main' into asyncio-runner
asvetlov Mar 22, 2022
674ad4e
Lazy init version
asvetlov Mar 22, 2022
7cd5430
Tune
asvetlov Mar 22, 2022
dd28ef7
Drop explicit get_context() function, asyncio.Task has no it also
asvetlov Mar 23, 2022
5e13b2e
Add docs for .close() method
asvetlov Mar 23, 2022
c0b999d
Add better error message for recursive run() call
asvetlov Mar 24, 2022
4937cd0
Add a note
asvetlov Mar 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tune
  • Loading branch information
asvetlov committed Mar 22, 2022
commit 7cd5430dccd89da0c2034f2cd9506ea2ff9cd610
14 changes: 12 additions & 2 deletions Doc/library/asyncio-runner.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,22 @@ Runner context manager

An optional keyword-only *context* argument allows specifying a
custom :class:`contextvars.Context` for the *coro* to run in.
The runner's context is used if ``None``.
The runner's default context is used if ``None``.

.. method:: close()

.. method:: get_loop()

Return the event loop associated with the runner instance.

.. method:: get_context()

Return the :class:`contextvars.Context` associated with the runner object.
Return the default :class:`contextvars.Context` associated with the runner object.

.. note::

:class:`Runner` uses the lazy initialization strategy, its constructor doesn't
initialize underlying low-level structures.

Embedded *loop* and *context* are created at :keyword:`with` body entering or the
first call of :meth:`run`, :meth:`get_loop`, or :meth:`get_context`.
19 changes: 10 additions & 9 deletions Lib/asyncio/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ def close(self):
self._loop = None
self._state = _State.CLOSED

def get_context(self):
"""Return the default associated context."""
self._lazy_init()
return self._context.copy()

def get_loop(self):
"""Return embedded event loop."""
self._lazy_init()
return self._loop

def run(self, coro, *, context=None):
"""Run a coroutine inside the embedded event loop."""
if not coroutines.iscoroutine(coro):
Expand All @@ -78,15 +88,6 @@ def run(self, coro, *, context=None):
task = self._loop.create_task(coro, context=context)
return self._loop.run_until_complete(task)

def get_loop(self):
"""Return embedded event loop."""
self._lazy_init()
return self._loop

def get_context(self):
self._lazy_init()
return self._context.copy()

def _lazy_init(self):
if self._state is _State.CLOSED:
raise RuntimeError("Runner is closed")
Expand Down