##// 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 import ast
14 import ast
15 import asyncio
15 import inspect
16 import inspect
16
17
17
18
18 class _AsyncIORunner:
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 def __call__(self, coro):
32 def __call__(self, coro):
20 """
33 """
21 Handler for asyncio autoawait
34 Handler for asyncio autoawait
22 """
35 """
23 import asyncio
36 return self.loop.run_until_complete(coro)
24
25 return asyncio.get_event_loop_policy().get_event_loop().run_until_complete(coro)
26
37
27 def __str__(self):
38 def __str__(self):
28 return "asyncio"
39 return "asyncio"
@@ -1058,6 +1058,22 b' def test_run_cell_async():'
1058 assert result.result == 5
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 def test_should_run_async():
1077 def test_should_run_async():
1062 assert not ip.should_run_async("a = 5")
1078 assert not ip.should_run_async("a = 5")
1063 assert ip.should_run_async("await x")
1079 assert ip.should_run_async("await x")
@@ -970,7 +970,11 b' def test_script_config():'
970
970
971 @pytest.fixture
971 @pytest.fixture
972 def event_loop():
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 @dec.skip_win32
980 @dec.skip_win32
@@ -504,13 +504,15 b' class TerminalInteractiveShell(InteractiveShell):'
504 # If we don't do this, people could spawn coroutine with a
504 # If we don't do this, people could spawn coroutine with a
505 # while/true inside which will freeze the prompt.
505 # while/true inside which will freeze the prompt.
506
506
507 policy = asyncio.get_event_loop_policy()
507 try:
508 try:
508 old_loop = asyncio.get_running_loop()
509 old_loop = policy.get_event_loop()
509 except RuntimeError:
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 old_loop = None
513 old_loop = None
512
514
513 asyncio.set_event_loop(self.pt_loop)
515 policy.set_event_loop(self.pt_loop)
514 try:
516 try:
515 with patch_stdout(raw=True):
517 with patch_stdout(raw=True):
516 text = self.pt_app.prompt(
518 text = self.pt_app.prompt(
@@ -518,7 +520,8 b' class TerminalInteractiveShell(InteractiveShell):'
518 **self._extra_prompt_options())
520 **self._extra_prompt_options())
519 finally:
521 finally:
520 # Restore the original event loop.
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 return text
526 return text
524
527
General Comments 0
You need to be logged in to leave comments. Login now