Skip to content

Commit 28b9178

Browse files
authored
bpo-32436: Document PEP 567 changes to asyncio. (GH-7073)
1 parent b6dccf5 commit 28b9178

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

Doc/library/asyncio-eventloop.rst

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ keywords to your callback, use :func:`functools.partial`. For example,
124124
parameters in debug mode, whereas ``lambda`` functions have a poor
125125
representation.
126126

127-
.. method:: AbstractEventLoop.call_soon(callback, \*args)
127+
.. method:: AbstractEventLoop.call_soon(callback, *args, context=None)
128128

129129
Arrange for a callback to be called as soon as possible. The callback is
130130
called after :meth:`call_soon` returns, when control returns to the event
@@ -137,19 +137,31 @@ keywords to your callback, use :func:`functools.partial`. For example,
137137
Any positional arguments after the callback will be passed to the
138138
callback when it is called.
139139

140+
An optional keyword-only *context* argument allows specifying a custom
141+
:class:`contextvars.Context` for the *callback* to run in. The current
142+
context is used when no *context* is provided.
143+
140144
An instance of :class:`asyncio.Handle` is returned, which can be
141145
used to cancel the callback.
142146

143147
:ref:`Use functools.partial to pass keywords to the callback
144148
<asyncio-pass-keywords>`.
145149

146-
.. method:: AbstractEventLoop.call_soon_threadsafe(callback, \*args)
150+
.. versionchanged:: 3.7
151+
The *context* keyword-only parameter was added. See :pep:`567`
152+
for more details.
153+
154+
.. method:: AbstractEventLoop.call_soon_threadsafe(callback, *args, context=None)
147155

148156
Like :meth:`call_soon`, but thread safe.
149157

150158
See the :ref:`concurrency and multithreading <asyncio-multithreading>`
151159
section of the documentation.
152160

161+
.. versionchanged:: 3.7
162+
The *context* keyword-only parameter was added. See :pep:`567`
163+
for more details.
164+
153165

154166
.. _asyncio-delayed-calls:
155167

@@ -166,7 +178,7 @@ a different clock than :func:`time.time`.
166178
Timeouts (relative *delay* or absolute *when*) should not exceed one day.
167179

168180

169-
.. method:: AbstractEventLoop.call_later(delay, callback, *args)
181+
.. method:: AbstractEventLoop.call_later(delay, callback, *args, context=None)
170182

171183
Arrange for the *callback* to be called after the given *delay*
172184
seconds (either an int or float).
@@ -182,10 +194,18 @@ a different clock than :func:`time.time`.
182194
is called. If you want the callback to be called with some named
183195
arguments, use a closure or :func:`functools.partial`.
184196

197+
An optional keyword-only *context* argument allows specifying a custom
198+
:class:`contextvars.Context` for the *callback* to run in. The current
199+
context is used when no *context* is provided.
200+
185201
:ref:`Use functools.partial to pass keywords to the callback
186202
<asyncio-pass-keywords>`.
187203

188-
.. method:: AbstractEventLoop.call_at(when, callback, *args)
204+
.. versionchanged:: 3.7
205+
The *context* keyword-only parameter was added. See :pep:`567`
206+
for more details.
207+
208+
.. method:: AbstractEventLoop.call_at(when, callback, *args, context=None)
189209

190210
Arrange for the *callback* to be called at the given absolute timestamp
191211
*when* (an int or float), using the same time reference as
@@ -199,6 +219,10 @@ a different clock than :func:`time.time`.
199219
:ref:`Use functools.partial to pass keywords to the callback
200220
<asyncio-pass-keywords>`.
201221

222+
.. versionchanged:: 3.7
223+
The *context* keyword-only parameter was added. See :pep:`567`
224+
for more details.
225+
202226
.. method:: AbstractEventLoop.time()
203227

204228
Return the current time, as a :class:`float` value, according to the

Doc/library/asyncio-task.rst

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,19 +275,27 @@ Future
275275
:exc:`CancelledError`. If the future isn't done yet, raises
276276
:exc:`InvalidStateError`.
277277

278-
.. method:: add_done_callback(fn)
278+
.. method:: add_done_callback(callback, *, context=None)
279279

280280
Add a callback to be run when the future becomes done.
281281

282-
The callback is called with a single argument - the future object. If the
282+
The *callback* is called with a single argument - the future object. If the
283283
future is already done when this is called, the callback is scheduled
284284
with :meth:`~AbstractEventLoop.call_soon`.
285285

286+
An optional keyword-only *context* argument allows specifying a custom
287+
:class:`contextvars.Context` for the *callback* to run in. The current
288+
context is used when no *context* is provided.
289+
286290
:ref:`Use functools.partial to pass parameters to the callback
287291
<asyncio-pass-keywords>`. For example,
288292
``fut.add_done_callback(functools.partial(print, "Future:",
289293
flush=True))`` will call ``print("Future:", fut, flush=True)``.
290294

295+
.. versionchanged:: 3.7
296+
The *context* keyword-only parameter was added. See :pep:`567`
297+
for more details.
298+
291299
.. method:: remove_done_callback(fn)
292300

293301
Remove all instances of a callback from the "call when done" list.
@@ -421,8 +429,15 @@ Task
421429
Don't directly create :class:`Task` instances: use the :func:`create_task`
422430
function or the :meth:`AbstractEventLoop.create_task` method.
423431

432+
Tasks support the :mod:`contextvars` module. When a Task
433+
is created it copies the current context and later runs its coroutine
434+
in the copied context. See :pep:`567` for more details.
435+
424436
This class is :ref:`not thread safe <asyncio-multithreading>`.
425437

438+
.. versionchanged:: 3.7
439+
Added support for the :mod:`contextvars` module.
440+
426441
.. classmethod:: all_tasks(loop=None)
427442

428443
Return a set of all tasks for an event loop.

Doc/whatsnew/3.7.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,17 @@ include:
608608
destroying the event loop.
609609
(Contributed by Yury Selivanov in :issue:`32314`.)
610610

611+
* asyncio gained support for :mod:`contextvars`.
612+
:meth:`loop.call_soon() <asyncio.AbstractEventLoop.call_soon>`,
613+
:meth:`loop.call_soon_threadsafe() <asyncio.AbstractEventLoop.call_soon_threadsafe>`,
614+
:meth:`loop.call_later() <asyncio.AbstractEventLoop.call_later>`,
615+
:meth:`loop.call_at() <asyncio.AbstractEventLoop.call_at>`, and
616+
:meth:`Future.add_done_callback() <asyncio.Future.add_done_callback>`
617+
have a new optional keyword-only *context* parameter.
618+
:class:`Tasks <asyncio.Task>` now track their context automatically.
619+
See :pep:`567` for more details.
620+
(Contributed by Yury Selivanov in :issue:`32436`.)
621+
611622
* The new :func:`asyncio.create_task` function has been added as a shortcut
612623
to ``asyncio.get_event_loop().create_task()``.
613624
(Contributed by Andrew Svetlov in :issue:`32311`.)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Document PEP 567 changes to asyncio.

0 commit comments

Comments
 (0)