##// END OF EJS Templates
avoid deprecated asyncio.get_event_loop...
Min RK -
Show More
@@ -12,17 +12,28 b' Python semantics.'
12 12
13 13
14 14 import ast
15 import asyncio
15 16 import inspect
16 17
17 18
18 19 class _AsyncIORunner:
20 def __init__(self):
21 self._loop = None
22
23 @property
24 def loop(self):
25 """Always returns a non-closed event loop"""
26 if self._loop is None or self._loop.is_closed():
27 policy = asyncio.get_event_loop_policy()
28 self._loop = policy.new_event_loop()
29 policy.set_event_loop(self._loop)
30 return self._loop
31
19 32 def __call__(self, coro):
20 33 """
21 34 Handler for asyncio autoawait
22 35 """
23 import asyncio
24
25 return asyncio.get_event_loop_policy().get_event_loop().run_until_complete(coro)
36 return self.loop.run_until_complete(coro)
26 37
27 38 def __str__(self):
28 39 return "asyncio"
@@ -1058,6 +1058,22 b' def test_run_cell_async():'
1058 1058 assert result.result == 5
1059 1059
1060 1060
1061 def test_run_cell_await():
1062 ip.run_cell("import asyncio")
1063 result = ip.run_cell("await asyncio.sleep(0.01); 10")
1064 assert ip.user_ns["_"] == 10
1065
1066
1067 def test_run_cell_asyncio_run():
1068 ip.run_cell("import asyncio")
1069 result = ip.run_cell("await asyncio.sleep(0.01); 1")
1070 assert ip.user_ns["_"] == 1
1071 result = ip.run_cell("asyncio.run(asyncio.sleep(0.01)); 2")
1072 assert ip.user_ns["_"] == 2
1073 result = ip.run_cell("await asyncio.sleep(0.01); 3")
1074 assert ip.user_ns["_"] == 3
1075
1076
1061 1077 def test_should_run_async():
1062 1078 assert not ip.should_run_async("a = 5")
1063 1079 assert ip.should_run_async("await x")
@@ -970,7 +970,11 b' def test_script_config():'
970 970
971 971 @pytest.fixture
972 972 def event_loop():
973 yield asyncio.get_event_loop_policy().get_event_loop()
973 policy = asyncio.get_event_loop_policy()
974 loop = policy.new_event_loop()
975 policy.set_event_loop(loop)
976 yield loop
977 loop.close()
974 978
975 979
976 980 @dec.skip_win32
@@ -504,13 +504,15 b' class TerminalInteractiveShell(InteractiveShell):'
504 504 # If we don't do this, people could spawn coroutine with a
505 505 # while/true inside which will freeze the prompt.
506 506
507 policy = asyncio.get_event_loop_policy()
507 508 try:
508 old_loop = asyncio.get_running_loop()
509 old_loop = policy.get_event_loop()
509 510 except RuntimeError:
510 # This happens when the user used `asyncio.run()`.
511 # This happens when the the event loop is closed,
512 # e.g. by calling `asyncio.run()`.
511 513 old_loop = None
512 514
513 asyncio.set_event_loop(self.pt_loop)
515 policy.set_event_loop(self.pt_loop)
514 516 try:
515 517 with patch_stdout(raw=True):
516 518 text = self.pt_app.prompt(
@@ -518,7 +520,8 b' class TerminalInteractiveShell(InteractiveShell):'
518 520 **self._extra_prompt_options())
519 521 finally:
520 522 # Restore the original event loop.
521 asyncio.set_event_loop(old_loop)
523 if old_loop is not None:
524 policy.set_event_loop(old_loop)
522 525
523 526 return text
524 527
General Comments 0
You need to be logged in to leave comments. Login now