Skip to content

Commit d8fc9c8

Browse files
Add missing asyncio changes from 3.8 whatsnew (GH-16911)
(cherry picked from commit 3bbb6db) Co-authored-by: Kyle Stanley <[email protected]>
1 parent 229335c commit d8fc9c8

File tree

1 file changed

+85
-10
lines changed

1 file changed

+85
-10
lines changed

Doc/whatsnew/3.8.rst

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,40 @@ The :func:`ast.parse` function has some new flags:
597597
asyncio
598598
-------
599599

600+
:func:`asyncio.run` has graduated from the provisional to stable API. This
601+
function can be used to execute a :term:`coroutine` and return the result while
602+
automatically managing the event loop. For example::
603+
604+
import asyncio
605+
606+
async def main():
607+
await asyncio.sleep(0)
608+
return 42
609+
610+
asyncio.run(main())
611+
612+
This is *roughly* equivalent to::
613+
614+
import asyncio
615+
616+
async def main():
617+
await asyncio.sleep(0)
618+
return 42
619+
620+
loop = asyncio.new_event_loop()
621+
asyncio.set_event_loop(loop)
622+
try:
623+
loop.run_until_complete(main())
624+
finally:
625+
asyncio.set_event_loop(None)
626+
loop.close()
627+
628+
629+
The actual implementation is significantly more complex. Thus,
630+
:func:`asyncio.run` should be the preferred way of running asyncio programs.
631+
632+
(Contributed by Yury Selivanov in :issue:`32314`.)
633+
600634
Running ``python -m asyncio`` launches a natively async REPL. This allows rapid
601635
experimentation with code that has a top-level :keyword:`await`. There is no
602636
longer a need to directly call ``asyncio.run()`` which would spawn a new event
@@ -614,6 +648,10 @@ loop on every invocation:
614648
615649
(Contributed by Yury Selivanov in :issue:`37028`.)
616650

651+
The exception :class:`asyncio.CancelledError` now inherits from
652+
:class:`BaseException` rather than :class:`Exception`.
653+
(Contributed by Yury Selivanov in :issue:`32528`.)
654+
617655
On Windows, the default event loop is now :class:`~asyncio.ProactorEventLoop`.
618656
(Contributed by Victor Stinner in :issue:`34687`.)
619657

@@ -624,6 +662,26 @@ On Windows, the default event loop is now :class:`~asyncio.ProactorEventLoop`.
624662
:exc:`KeyboardInterrupt` ("CTRL+C").
625663
(Contributed by Vladimir Matveev in :issue:`23057`.)
626664

665+
Added :meth:`asyncio.Task.get_coro` for getting the wrapped coroutine
666+
within an :class:`asyncio.Task`.
667+
(Contributed by Alex Grönholm in :issue:`36999`.)
668+
669+
Asyncio tasks can now be named, either by passing the ``name`` keyword
670+
argument to :func:`asyncio.create_task` or
671+
the :meth:`~asyncio.loop.create_task` event loop method, or by
672+
calling the :meth:`~asyncio.Task.set_name` method on the task object. The
673+
task name is visible in the ``repr()`` output of :class:`asyncio.Task` and
674+
can also be retrieved using the :meth:`~asyncio.Task.get_name` method.
675+
(Contributed by Alex Grönholm in :issue:`34270`.)
676+
677+
Added support for
678+
`Happy Eyeballs <https://en.wikipedia.org/wiki/Happy_Eyeballs>`_ to
679+
:func:`asyncio.loop.create_connection`. To specify the behavior, two new
680+
parameters have been added: *happy_eyeballs_delay* and *interleave*. The Happy
681+
Eyeballs algorithm improves responsiveness in applications that support IPv4
682+
and IPv6 by attempting to simultaneously connect using both.
683+
(Contributed by twisteroid ambassador in :issue:`33530`.)
684+
627685

628686
builtins
629687
--------
@@ -1577,7 +1635,7 @@ Deprecated
15771635

15781636
* Passing an object that is not an instance of
15791637
:class:`concurrent.futures.ThreadPoolExecutor` to
1580-
:meth:`asyncio.loop.set_default_executor()` is
1638+
:meth:`loop.set_default_executor() <asyncio.loop.set_default_executor>` is
15811639
deprecated and will be prohibited in Python 3.9.
15821640
(Contributed by Elvis Pranskevichus in :issue:`34075`.)
15831641

@@ -1610,6 +1668,19 @@ Deprecated
16101668
:keyword:`async def` instead.
16111669
(Contributed by Andrew Svetlov in :issue:`36921`.)
16121670

1671+
* In :mod:`asyncio`, the explicit passing of a *loop* argument has been
1672+
deprecated and will be removed in version 3.10 for the following:
1673+
:func:`asyncio.sleep`, :func:`asyncio.gather`, :func:`asyncio.shield`,
1674+
:func:`asyncio.wait_for`, :func:`asyncio.wait`, :func:`asyncio.as_completed`,
1675+
:class:`asyncio.Task`, :class:`asyncio.Lock`, :class:`asyncio.Event`,
1676+
:class:`asyncio.Condition`, :class:`asyncio.Semaphore`,
1677+
:class:`asyncio.BoundedSemaphore`, :class:`asyncio.Queue`,
1678+
:func:`asyncio.create_subprocess_exec`, and
1679+
:func:`asyncio.create_subprocess_shell`.
1680+
1681+
* The explicit passing of coroutine objects to :func:`asyncio.wait` has been
1682+
deprecated and will be removed in version 3.10.
1683+
16131684
* The following functions and methods are deprecated in the :mod:`gettext`
16141685
module: :func:`~gettext.lgettext`, :func:`~gettext.ldgettext`,
16151686
:func:`~gettext.lngettext` and :func:`~gettext.ldngettext`.
@@ -1854,13 +1925,6 @@ Changes in the Python API
18541925
you adjust (possibly including adding accessor functions to the
18551926
public API). (See :issue:`35886`.)
18561927

1857-
* Asyncio tasks can now be named, either by passing the ``name`` keyword
1858-
argument to :func:`asyncio.create_task` or
1859-
the :meth:`~asyncio.loop.create_task` event loop method, or by
1860-
calling the :meth:`~asyncio.Task.set_name` method on the task object. The
1861-
task name is visible in the ``repr()`` output of :class:`asyncio.Task` and
1862-
can also be retrieved using the :meth:`~asyncio.Task.get_name` method.
1863-
18641928
* The :meth:`mmap.flush() <mmap.mmap.flush>` method now returns ``None`` on
18651929
success and raises an exception on error under all platforms. Previously,
18661930
its behavior was platform-dependent: a nonzero value was returned on success;
@@ -1883,8 +1947,19 @@ Changes in the Python API
18831947
(Contributed by Anthony Sottile in :issue:`36264`.)
18841948

18851949
* The exception :class:`asyncio.CancelledError` now inherits from
1886-
:class:`BaseException` rather than a :class:`Exception`.
1887-
(Contributed by Yury Selivanov in :issue:`13528`.)
1950+
:class:`BaseException` rather than :class:`Exception`.
1951+
(Contributed by Yury Selivanov in :issue:`32528`.)
1952+
1953+
* The function :func:`asyncio.wait_for` now correctly waits for cancellation
1954+
when using an instance of :class:`asyncio.Task`. Previously, upon reaching
1955+
*timeout*, it was cancelled and immediately returned.
1956+
(Contributed by Elvis Pranskevichus in :issue:`32751`.)
1957+
1958+
* The function :func:`asyncio.BaseTransport.get_extra_info` now returns a safe
1959+
to use socket object when 'socket' is passed to the *name* parameter.
1960+
(Contributed by Yury Selivanov in :issue:`37027`.)
1961+
1962+
* :class:`asyncio.BufferedProtocol` has graduated to the stable API.
18881963

18891964
.. _bpo-36085-whatsnew:
18901965

0 commit comments

Comments
 (0)