Blaszard
Blaszard

Reputation: 31955

How can I get to run `loop.run_until_complete()` itself asynchronously on asyncio?

In the following code:

loop = asyncio.get_event_loop()
l_x = map(async_func, data_x)
l_y = map(async_func, data_y)
l_z = map(async_func, data_z)
x = loop.run_until_complete(asyncio.gather(*l_x))
y = loop.run_until_complete(asyncio.gather(*l_y))
z = loop.run_until_complete(asyncio.gather(*l_z))

The async_func includes the I/O task and takes a bit longer, so I'm going to use the asyncio to run it concurrently.

However, the problem is that I found it is blocking the execution after the first loop.run_until_complete(), and only after all the execution on l_x are done, it then goes into the execution of the second loop.run_until_complete().

But I rather want to run all of them simultaneously, and also don't like to merge the three results as all of them are irrelevant. In this case, can I still run all of them concurrently?

Upvotes: 2

Views: 262

Answers (1)

Brandon Krueger
Brandon Krueger

Reputation: 156

You could run these coroutines concurrently by queuing them as follows:

l1 = asyncio.gather(*l_x)
l2 = asyncio.gather(*l_y)
l3 = asyncio.gather(*l_z)

results = loop.run_until_complete(asyncio.gather(l1, l2, l3))

If you would like to retain the results of each coroutine separately, then you could have each coroutine assign the results to some class level instance variables.

Upvotes: 3

Related Questions