@@ -597,6 +597,40 @@ The :func:`ast.parse` function has some new flags:
597
597
asyncio
598
598
-------
599
599
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
+
600
634
Running ``python -m asyncio `` launches a natively async REPL. This allows rapid
601
635
experimentation with code that has a top-level :keyword: `await `. There is no
602
636
longer a need to directly call ``asyncio.run() `` which would spawn a new event
@@ -614,6 +648,10 @@ loop on every invocation:
614
648
615
649
(Contributed by Yury Selivanov in :issue: `37028 `.)
616
650
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
+
617
655
On Windows, the default event loop is now :class: `~asyncio.ProactorEventLoop `.
618
656
(Contributed by Victor Stinner in :issue: `34687 `.)
619
657
@@ -624,6 +662,26 @@ On Windows, the default event loop is now :class:`~asyncio.ProactorEventLoop`.
624
662
:exc: `KeyboardInterrupt ` ("CTRL+C").
625
663
(Contributed by Vladimir Matveev in :issue: `23057 `.)
626
664
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
+
627
685
628
686
builtins
629
687
--------
@@ -1577,7 +1635,7 @@ Deprecated
1577
1635
1578
1636
* Passing an object that is not an instance of
1579
1637
: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
1581
1639
deprecated and will be prohibited in Python 3.9.
1582
1640
(Contributed by Elvis Pranskevichus in :issue: `34075 `.)
1583
1641
@@ -1610,6 +1668,19 @@ Deprecated
1610
1668
:keyword: `async def ` instead.
1611
1669
(Contributed by Andrew Svetlov in :issue: `36921 `.)
1612
1670
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
+
1613
1684
* The following functions and methods are deprecated in the :mod: `gettext `
1614
1685
module: :func: `~gettext.lgettext `, :func: `~gettext.ldgettext `,
1615
1686
:func: `~gettext.lngettext ` and :func: `~gettext.ldngettext `.
@@ -1854,13 +1925,6 @@ Changes in the Python API
1854
1925
you adjust (possibly including adding accessor functions to the
1855
1926
public API). (See :issue: `35886 `.)
1856
1927
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
-
1864
1928
* The :meth: `mmap.flush() <mmap.mmap.flush> ` method now returns ``None `` on
1865
1929
success and raises an exception on error under all platforms. Previously,
1866
1930
its behavior was platform-dependent: a nonzero value was returned on success;
@@ -1883,8 +1947,19 @@ Changes in the Python API
1883
1947
(Contributed by Anthony Sottile in :issue: `36264 `.)
1884
1948
1885
1949
* 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.
1888
1963
1889
1964
.. _bpo-36085-whatsnew :
1890
1965
0 commit comments