diff options
author | Yury Selivanov <yury@magic.io> | 2017-12-14 14:42:21 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-14 14:42:21 (GMT) |
commit | 02a0a19206da6902c3855a1fa09e60b208474cfa (patch) | |
tree | df9a24bf2a131693ef4f3dad55849c22a1567991 /Doc/library | |
parent | eadad1b97f64619bfd246b9d3b60d25f456e0592 (diff) | |
download | cpython-02a0a19206da6902c3855a1fa09e60b208474cfa.zip cpython-02a0a19206da6902c3855a1fa09e60b208474cfa.tar.gz cpython-02a0a19206da6902c3855a1fa09e60b208474cfa.tar.bz2 |
bpo-32314: Implement asyncio.run() (#4852)
Diffstat (limited to 'Doc/library')
-rw-r--r-- | Doc/library/asyncio-task.rst | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index a8a0a8e..0d0569f0 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -92,6 +92,24 @@ Coroutines (and tasks) can only run when the event loop is running. used in a callback-style code, wrap its result with :func:`ensure_future`. +.. function:: asyncio.run(coro, \*, debug=False) + + This function runs the passed coroutine, taking care of + managing the asyncio event loop and finalizing asynchronous + generators. + + This function cannot be called when another asyncio event loop is + running in the same thread. + + If debug is True, the event loop will be run in debug mode. + + This function always creates a new event loop and closes it at + the end. It should be used as a main entry point for asyncio + programs, and should ideally only be called once. + + .. versionadded:: 3.7 + + .. _asyncio-hello-world-coroutine: Example: Hello World coroutine @@ -104,10 +122,7 @@ Example of coroutine displaying ``"Hello World"``:: async def hello_world(): print("Hello World!") - loop = asyncio.get_event_loop() - # Blocking call which returns when the hello_world() coroutine is done - loop.run_until_complete(hello_world()) - loop.close() + asyncio.run(hello_world()) .. seealso:: @@ -127,7 +142,8 @@ using the :meth:`sleep` function:: import asyncio import datetime - async def display_date(loop): + async def display_date(): + loop = asyncio.get_running_loop() end_time = loop.time() + 5.0 while True: print(datetime.datetime.now()) @@ -135,10 +151,7 @@ using the :meth:`sleep` function:: break await asyncio.sleep(1) - loop = asyncio.get_event_loop() - # Blocking call which returns when the display_date() coroutine is done - loop.run_until_complete(display_date(loop)) - loop.close() + asyncio.run(display_date()) .. seealso:: |