| 1 |
""" |
| 2 |
Inputhook for running the original asyncio event loop while we're waiting for |
| 3 |
input. |
| 4 |
|
| 5 |
By default, in IPython, we run the prompt with a different asyncio event loop, |
| 6 |
because otherwise we risk that people are freezing the prompt by scheduling bad |
| 7 |
coroutines. E.g., a coroutine that does a while/true and never yield back |
| 8 |
control to the loop. We can't cancel that. |
| 9 |
|
| 10 |
However, sometimes we want the asyncio loop to keep running while waiting for |
| 11 |
a prompt. |
| 12 |
|
| 13 |
The following example will print the numbers from 1 to 10 above the prompt, |
| 14 |
while we are waiting for input. (This works also because we use |
| 15 |
prompt_toolkit`s `patch_stdout`):: |
| 16 |
|
| 17 |
In [1]: import asyncio |
| 18 |
|
| 19 |
In [2]: %gui asyncio |
| 20 |
|
| 21 |
In [3]: async def f(): |
| 22 |
...: for i in range(10): |
| 23 |
...: await asyncio.sleep(1) |
| 24 |
...: print(i) |
| 25 |
|
| 26 |
|
| 27 |
In [4]: asyncio.ensure_future(f()) |
| 28 |
|
| 29 |
""" |
| 30 |
|
| 31 |
|
| 32 |
def inputhook(context): |
| 33 |
""" |
| 34 |
Inputhook for asyncio event loop integration. |
| 35 |
""" |
| 36 |
# For prompt_toolkit 3.0, this input hook literally doesn't do anything. |
| 37 |
# The event loop integration here is implemented in `interactiveshell.py` |
| 38 |
# by running the prompt itself in the current asyncio loop. The main reason |
| 39 |
# for this is that nesting asyncio event loops is unreliable. |
| 40 |
return |