summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorThomas Grainger <tagrain@gmail.com>2024-12-18 08:20:05 (GMT)
committerGitHub <noreply@github.com>2024-12-18 08:20:05 (GMT)
commit0581e3f52b6116774698761088f0daa7ec23fc0b (patch)
tree574d2ba5f70994cf2de72f6e27bae376adc47a9b /Doc
parent329165639f9ac00ba64f6493dbcafcef6955e2cb (diff)
downloadcpython-0581e3f52b6116774698761088f0daa7ec23fc0b.zip
cpython-0581e3f52b6116774698761088f0daa7ec23fc0b.tar.gz
cpython-0581e3f52b6116774698761088f0daa7ec23fc0b.tar.bz2
gh-127174: add docs for asyncio.get_event_loop replacements (#127640)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Diffstat (limited to 'Doc')
-rw-r--r--Doc/whatsnew/3.14.rst90
1 files changed, 90 insertions, 0 deletions
diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst
index d13cd2d..342456c 100644
--- a/Doc/whatsnew/3.14.rst
+++ b/Doc/whatsnew/3.14.rst
@@ -780,6 +780,96 @@ asyncio
It now raises a :exc:`RuntimeError` if there is no current event loop.
(Contributed by Kumar Aditya in :gh:`126353`.)
+ There's a few patterns that use :func:`asyncio.get_event_loop`, most
+ of them can be replaced with :func:`asyncio.run`.
+
+ If you're running an async function, simply use :func:`asyncio.run`.
+
+ Before::
+
+ async def main():
+ ...
+
+
+ loop = asyncio.get_event_loop()
+ try:
+ loop.run_until_complete(main())
+ finally:
+ loop.close()
+
+ After::
+
+ async def main():
+ ...
+
+ asyncio.run(main())
+
+ If you need to start something, e.g. a server listening on a socket
+ and then run forever, use :func:`asyncio.run` and an
+ :class:`asyncio.Event`.
+
+ Before::
+
+ def start_server(loop):
+ ...
+
+ loop = asyncio.get_event_loop()
+ try:
+ start_server(loop)
+ loop.run_forever()
+ finally:
+ loop.close()
+
+ After::
+
+ def start_server(loop):
+ ...
+
+ async def main():
+ start_server(asyncio.get_running_loop())
+ await asyncio.Event().wait()
+
+ asyncio.run(main())
+
+ If you need to run something in an event loop, then run some blocking
+ code around it, use :class:`asyncio.Runner`.
+
+ Before::
+
+ async def operation_one():
+ ...
+
+ def blocking_code():
+ ...
+
+ async def operation_two():
+ ...
+
+ loop = asyncio.get_event_loop()
+ try:
+ loop.run_until_complete(operation_one())
+ blocking_code()
+ loop.run_until_complete(operation_two())
+ finally:
+ loop.close()
+
+ After::
+
+ async def operation_one():
+ ...
+
+ def blocking_code():
+ ...
+
+ async def operation_two():
+ ...
+
+ with asyncio.Runner() as runner:
+ runner.run(operation_one())
+ blocking_code()
+ runner.run(operation_two())
+
+
collections.abc
---------------