Show More
@@ -13,19 +13,29 b' Python semantics.' | |||
|
13 | 13 | |
|
14 | 14 | import ast |
|
15 | 15 | import sys |
|
16 | import asyncio | |
|
16 | 17 | import inspect |
|
17 | 18 | from textwrap import dedent, indent |
|
18 | 19 | |
|
19 | 20 | |
|
20 | 21 | class _AsyncIORunner: |
|
22 | def __init__(self): | |
|
23 | self._loop = None | |
|
24 | ||
|
25 | @property | |
|
26 | def loop(self): | |
|
27 | """Always returns a non-closed event loop""" | |
|
28 | if self._loop is None or self._loop.is_closed(): | |
|
29 | policy = asyncio.get_event_loop_policy() | |
|
30 | self._loop = policy.new_event_loop() | |
|
31 | policy.set_event_loop(self._loop) | |
|
32 | return self._loop | |
|
21 | 33 | |
|
22 | 34 | def __call__(self, coro): |
|
23 | 35 | """ |
|
24 | 36 | Handler for asyncio autoawait |
|
25 | 37 | """ |
|
26 | import asyncio | |
|
27 | ||
|
28 | return asyncio.get_event_loop().run_until_complete(coro) | |
|
38 | return self.loop.run_until_complete(coro) | |
|
29 | 39 | |
|
30 | 40 | def __str__(self): |
|
31 | 41 | return 'asyncio' |
@@ -1037,6 +1037,22 b' def test_run_cell_async():' | |||
|
1037 | 1037 | assert result.result == 5 |
|
1038 | 1038 | |
|
1039 | 1039 | |
|
1040 | def test_run_cell_await(): | |
|
1041 | ip.run_cell("import asyncio") | |
|
1042 | result = ip.run_cell("await asyncio.sleep(0.01); 10") | |
|
1043 | assert ip.user_ns["_"] == 10 | |
|
1044 | ||
|
1045 | ||
|
1046 | def test_run_cell_asyncio_run(): | |
|
1047 | ip.run_cell("import asyncio") | |
|
1048 | result = ip.run_cell("await asyncio.sleep(0.01); 1") | |
|
1049 | assert ip.user_ns["_"] == 1 | |
|
1050 | result = ip.run_cell("asyncio.run(asyncio.sleep(0.01)); 2") | |
|
1051 | assert ip.user_ns["_"] == 2 | |
|
1052 | result = ip.run_cell("await asyncio.sleep(0.01); 3") | |
|
1053 | assert ip.user_ns["_"] == 3 | |
|
1054 | ||
|
1055 | ||
|
1040 | 1056 | def test_should_run_async(): |
|
1041 | 1057 | assert not ip.should_run_async("a = 5") |
|
1042 | 1058 | assert ip.should_run_async("await x") |
@@ -460,13 +460,15 b' class TerminalInteractiveShell(InteractiveShell):' | |||
|
460 | 460 | # If we don't do this, people could spawn coroutine with a |
|
461 | 461 | # while/true inside which will freeze the prompt. |
|
462 | 462 | |
|
463 | policy = asyncio.get_event_loop_policy() | |
|
463 | 464 | try: |
|
464 |
old_loop = |
|
|
465 | old_loop = policy.get_event_loop() | |
|
465 | 466 | except RuntimeError: |
|
466 |
# This happens when the |
|
|
467 | # This happens when the the event loop is closed, | |
|
468 | # e.g. by calling `asyncio.run()`. | |
|
467 | 469 | old_loop = None |
|
468 | 470 | |
|
469 |
|
|
|
471 | policy.set_event_loop(self.pt_loop) | |
|
470 | 472 | try: |
|
471 | 473 | with patch_stdout(raw=True): |
|
472 | 474 | text = self.pt_app.prompt( |
@@ -474,7 +476,8 b' class TerminalInteractiveShell(InteractiveShell):' | |||
|
474 | 476 | **self._extra_prompt_options()) |
|
475 | 477 | finally: |
|
476 | 478 | # Restore the original event loop. |
|
477 | asyncio.set_event_loop(old_loop) | |
|
479 | if old_loop is not None: | |
|
480 | policy.set_event_loop(old_loop) | |
|
478 | 481 | |
|
479 | 482 | return text |
|
480 | 483 |
General Comments 0
You need to be logged in to leave comments.
Login now