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())
With the aysncio library you can use aysncio.gather()
This can come in handy if you are also sending HTTP requests in parallel:
Looking at the desired output, it seems that the goal is to leave the individual iteration as it is - i.e. run
first
andsecond
sequentially - but execute both loop iterations in parallel.Assuming you only want to modify
main()
, it could be achieved like this: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.To run the two functions simultaneously you can use
gather
. However, the results will be provided to you in the order you provide them. So for example if you doThen you will get
[the result of first(), the result of second()]
back. If you want to do something whenever each one returns then you should useTasks
explicitly and add callbacks.