2

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.