-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Open
Labels
Description
I tried something like ipython/ipykernel#323 (comment) but it looks like "background" tasks are run only when await statement is executed?
Python 3.7.0 (default, Jul 15 2018, 10:44:58)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.0.0.dev -- An enhanced Interactive Python. Type '?' for help.
IPython profile: plain
In [1]: import asyncio
...:
...: async def aprint(*args):
...: print(*args)
...:
...: asyncio.ensure_future(aprint("background future"))
Out[1]: <Task pending coro=<aprint() running at <ipython-input-1-c3f9a8264358>:3>>
In [2]:
In [2]:
In [2]: await aprint("foreground")
background future
foregroundAlso, asyncio.create_task does not work:
In [3]: asyncio.create_task(aprint("background task"))
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-3-6787ab5b49b6> in <module>
----> 1 asyncio.create_task(aprint("background task"))
/usr/lib64/python3.7/asyncio/tasks.py in create_task(coro)
318 Return a Task object.
319 """
--> 320 loop = events.get_running_loop()
321 return loop.create_task(coro)
322
RuntimeError: no running event loopReading
ipython/docs/source/whatsnew/version7.rst
Lines 113 to 117 in 73d6335
| Non-Asynchronous code | |
| ~~~~~~~~~~~~~~~~~~~~~ | |
| As the internal API of IPython are now asynchronous, IPython need to run under | |
| an even loop. In order to allow many workflow, (like using the ``%run`` magic, |
I thought the event loop is always running. But it's not the case, right?
In [4]: asyncio.get_event_loop()
Out[4]: <_UnixSelectorEventLoop running=False closed=False debug=False>
In [5]: async def print_loop():
...: print(asyncio.get_event_loop())
In [6]: await print_loop()
<_UnixSelectorEventLoop running=True closed=False debug=False>I was expecting running=True in Out[4].
Checked with 73d6335