@@ -595,6 +595,40 @@ The :func:`ast.parse` function has some new flags:
595
595
asyncio
596
596
-------
597
597
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
+
598
632
Running ``python -m asyncio `` launches a natively async REPL. This allows rapid
599
633
experimentation with code that has a top-level :keyword: `await `. There is no
600
634
longer a need to directly call ``asyncio.run() `` which would spawn a new event
@@ -612,6 +646,10 @@ loop on every invocation:
612
646
613
647
(Contributed by Yury Selivanov in :issue: `37028 `.)
614
648
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
+
615
653
On Windows, the default event loop is now :class: `~asyncio.ProactorEventLoop `.
616
654
(Contributed by Victor Stinner in :issue: `34687 `.)
617
655
@@ -622,6 +660,26 @@ On Windows, the default event loop is now :class:`~asyncio.ProactorEventLoop`.
622
660
:exc: `KeyboardInterrupt ` ("CTRL+C").
623
661
(Contributed by Vladimir Matveev in :issue: `23057 `.)
624
662
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
+
625
683
626
684
builtins
627
685
--------
@@ -1575,7 +1633,7 @@ Deprecated
1575
1633
1576
1634
* Passing an object that is not an instance of
1577
1635
: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
1579
1637
deprecated and will be prohibited in Python 3.9.
1580
1638
(Contributed by Elvis Pranskevichus in :issue: `34075 `.)
1581
1639
@@ -1608,6 +1666,19 @@ Deprecated
1608
1666
:keyword: `async def ` instead.
1609
1667
(Contributed by Andrew Svetlov in :issue: `36921 `.)
1610
1668
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
+
1611
1682
* The following functions and methods are deprecated in the :mod: `gettext `
1612
1683
module: :func: `~gettext.lgettext `, :func: `~gettext.ldgettext `,
1613
1684
:func: `~gettext.lngettext ` and :func: `~gettext.ldngettext `.
@@ -1852,13 +1923,6 @@ Changes in the Python API
1852
1923
you adjust (possibly including adding accessor functions to the
1853
1924
public API). (See :issue: `35886 `.)
1854
1925
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
-
1862
1926
* The :meth: `mmap.flush() <mmap.mmap.flush> ` method now returns ``None `` on
1863
1927
success and raises an exception on error under all platforms. Previously,
1864
1928
its behavior was platform-dependent: a nonzero value was returned on success;
@@ -1881,8 +1945,19 @@ Changes in the Python API
1881
1945
(Contributed by Anthony Sottile in :issue: `36264 `.)
1882
1946
1883
1947
* 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.
1886
1961
1887
1962
.. _bpo-36085-whatsnew :
1888
1963
0 commit comments