Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making async for loops in Python

The following code outputs as follows:

1 sec delay, print "1", 
1 sec delay, print "2", 
1 sec delay, print "1", 
1 sec delay, print "2"

How can it be modified to run like this:

1 sec delay, print "1", print "1",
1 sec delay, print "2", print "2"

I would like it to run so that both instances of the for loop begin executing at the same time. As each instance executes, they will encounter the first() function at the same time, then the second() function at the same time, thus printing in the order mentioned above.

Code:

import asyncio

async def first():
    await asyncio.sleep(1)
    return "1"

async def second():
    await asyncio.sleep(1)
    return "2"

async def main():     
    for i in range(2):
      result = await first()
      print(result)
      result2 = await second()
      print(result2)


loop = asyncio.get_event_loop()
loop.run_until_complete(main())
like image 680
Canis Lobo Avatar asked Nov 26 '18 18:11

Canis Lobo


People also ask

Can you do async in Python?

It makes use of Python async features using asyncio/await provided in Python 3. The time and queue modules have been replaced with the asyncio package. This gives your program access to asynchronous friendly (non-blocking) sleep and queue functionality.

How do you create async function in Python?

To run an async function (coroutine) you have to call it using an Event Loop. Event Loops: You can think of Event Loop as functions to run asynchronous tasks and callbacks, perform network IO operations, and run subprocesses. Example 1: Event Loop example to run async Function to run a single async function: Python3.

Are for loops asynchronous?

For loops. Combining async with a for (or a for...of ) loop is possibly the most straightforward option when performing asynchronous operations over array elements. Using await inside a for loop will cause the code to stop and wait for the asynchronous operation to complete before continuing.

What is an async iterator Python?

Async iterators: In the regular iterators, what if there is a scenario where you have to do some I/O tasks to get data inside the __next__() method. That's exactly what async iterators do. An async iterator typically contains, A __aiter__() method instead of __iter__() method.


1 Answers

Looking at the desired output, it seems that the goal is to leave the individual iteration as it is - i.e. run first and second sequentially - but execute both loop iterations in parallel.

Assuming you only want to modify main(), it could be achieved like this:

async def main():
    async def one_iteration():
        result = await first()
        print(result)
        result2 = await second()
        print(result2)
    coros = [one_iteration() for _ in range(2)]
    await asyncio.gather(*coros)

Instead of iterating in sequence, the above creates a coroutine for each iteration task, and uses asyncio.gather to execute all the iterations in parallel.

Note that simply creating a coroutine doesn't start executing it, so a large number of coros won't block the event loop.

like image 116
user4815162342 Avatar answered Sep 19 '22 22:09

user4815162342