Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do most asyncio examples use loop.run_until_complete()?

I was going through the Python documentation for asyncio and I'm wondering why most examples use loop.run_until_complete() as opposed to Asyncio.ensure_future().

For example: https://docs.python.org/dev/library/asyncio-task.html

It seems ensure_future would be a much better way to demonstrate the advantages of non-blocking functions. run_until_complete on the other hand, blocks the loop like synchronous functions do.

This makes me feel like I should be using run_until_complete instead of a combination of ensure_futurewith loop.run_forever() to run multiple co-routines concurrently.

like image 421
SamuelN Avatar asked Oct 19 '16 23:10

SamuelN


People also ask

How many times should Asyncio run () be called?

It should be used as a main entry point for asyncio programs, and should ideally only be called once. New in version 3.7.

What is Asyncio event loop?

The event loop is the core of every asyncio application. Event loops run asynchronous tasks and callbacks, perform network IO operations, and run subprocesses. Application developers should typically use the high-level asyncio functions, such as asyncio.

What is Loop Run_in_executor?

run_in_executor is used to manage threads from within an event loop. To this end, it needs to wrap the thread into a Future, which needs to be assigned to an event loop (in one way or another). The reason the method is stored directly in a loop object is probably historical.

How do I stop Asyncio event loop?

Run an asyncio Event Loop run_until_complete(<some Future object>) – this function runs a given Future object, usually a coroutine defined by the async / await pattern, until it's complete. run_forever() – this function runs the loop forever. stop() – the stop function stops a running loop.


1 Answers

run_until_complete is used to run a future until it's finished. It will block the execution of code following it. It does, however, cause the event loop to run. Any futures that have been scheduled will run until the future passed to run_until_complete is done.

Given this example:

import asyncio  async def do_io():     print('io start')     await asyncio.sleep(5)     print('io end')  async def do_other_things():     print('doing other things')  loop = asyncio.get_event_loop()  loop.run_until_complete(do_io()) loop.run_until_complete(do_other_things())  loop.close() 

do_io will run. After it's complete, do_other_things will run. Your output will be:

io start io end doing other things 

If you schedule do_other_things with the event loop before running do_io, control will switch from do_io to do_other_things when the former awaits.

loop.create_task(do_other_things()) loop.run_until_complete(do_io()) 

This will get you the output of:

doing other things io start io end 

This is because do_other_things was scheduled before do_io. There are a lot of different ways to get the same output, but which one makes sense really depends on what your application actually does. So I'll leave that as an exercise to the reader.

like image 117
dirn Avatar answered Sep 28 '22 16:09

dirn