ucag
ucag

Reputation: 477

How to wait for task done during current running loop?

I want to wait for the result of a set of tasks during the current loop running in the Ipython notebook due to the need of data process. In the Ipython notebook, the event loop is running by default. That is to say, whenever u run the code below, you'll get True

import asyncio
loop = asyncio.get_event_loop()
loop.is_running()

And you can't shutdown the current loop because the notebook is using it. And I have some tasks to do in Ipython notebook. But I can't create a new loop without closing the current loop. I almost implement it. This is how I do it:

import asyncio as aio
loop = aio.get_event_loop()
async def hello():
    await aio.sleep(10)
    return "hello"
tasks = []
for i in range(10):
    tasks.append(loop.create_task(hello()))
    # all the tasks will automatically run
rest = aio.gather(*tasks)

Now what I need to do is to wait for rest to be done, however, I can't use loop.run_until_complete to wait for it to complete, because I can't close the current notebook loop.

What should I do to get the result? Thanks.

Upvotes: 3

Views: 1399

Answers (1)

Matt Messersmith
Matt Messersmith

Reputation: 13747

Just create a new event loop and set it as the current event loop. I couldn't reproduce your issue, see below, so note that this is untested:

import asyncio as aio
loop = aio.new_event_loop()
aio.set_event_loop(loop)

Then you should be able to safely call loop.run_until_complete(rest) without interfering with the default event loop.

See the docs for details: https://docs.python.org/3/library/asyncio-eventloop.html

I'm not sure what version of IPython/Jupyter notebook you're using, but I couldn't reproduce the issue on Jupyter notebook version 4.4 or IPython version 6.2.1, on Mac OS High Sierra. Note that IPython notebooks have long been deprecated in favor of Jupyter notebooks.

jMatthews-MacBook-Pro:stackoverflow matt$ jupyter --version
4.4.0
Matthews-MacBook-Pro:stackoverflow matt$ ipython --version
6.2.1

For instance, when running ipython REPL:

In [1]: import asyncio
   ...: loop = asyncio.get_event_loop()
   ...: loop.is_running()
   ...: 
Out[1]: False

Similar output in a Jupyter notebook.

Upvotes: 2

Related Questions