Skip to content

Commit 3bbb6db

Browse files
aeros1st1
authored andcommitted
Add missing asyncio changes from 3.8 whatsnew (GH-16911)
1 parent a01ba33 commit 3bbb6db

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
@@ -595,6 +595,40 @@ The :func:`ast.parse` function has some new flags:
595595
asyncio
596596
-------
597597

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

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

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

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

626684
builtins
627685
--------
@@ -1575,7 +1633,7 @@ Deprecated
15751633

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

@@ -1608,6 +1666,19 @@ Deprecated
16081666
:keyword:`async def` instead.
16091667
(Contributed by Andrew Svetlov in :issue:`36921`.)
16101668

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

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

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

18871962
.. _bpo-36085-whatsnew:
18881963

0 commit comments

Comments
 (0)