在异步代码中调用IPython.embed()(指定事件循环)
IPython 7.5文件规定: 更改为嵌套嵌入 引入运行异步代码的功能对IPython.embed()API产生了一些影响。默认情况下,嵌入 除非指定了事件循环,否则将不允许运行异步代码 但是,文档中似乎没有描述如何指定事件循环 运行:在异步代码中调用IPython.embed()(指定事件循环),python,ipython,Python,Ipython,IPython 7.5文件规定: 更改为嵌套嵌入 引入运行异步代码的功能对IPython.embed()API产生了一些影响。默认情况下,嵌入 除非指定了事件循环,否则将不允许运行异步代码 但是,文档中似乎没有描述如何指定事件循环 运行: 导入IPython IPython.embed() 然后 In [1]: %autoawait on In [2]: %autoawait IPython autoawait is `on`, and set to use `<function _p
导入IPython
IPython.embed()
然后
In [1]: %autoawait on
In [2]: %autoawait
IPython autoawait is `on`, and set to use `<function _pseudo_sync_runner at 0x00000000066DEF28>`
In [3]: import asyncio
In [4]: await asyncio.sleep(1)
另一方面,跑步:
导入IPython
嵌入(使用='asyncio')
然后:
In [1]: %autoawait on
In [2]: %autoawait
IPython autoawait is `on`, and set to use `asyncio`
In [3]: import asyncio
In [4]: await asyncio.sleep(1)
给出:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
~\Envs\[redacted]\lib\site-packages\IPython\core\async_helpers.py in _pseudo_sync_runner(coro)
71 # TODO: do not raise but return an execution result with the right info.
72 raise RuntimeError(
---> 73 "{coro_name!r} needs a real async loop".format(coro_name=coro.__name__)
74 )
75
RuntimeError: 'run_cell_async' needs a real async loop
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
~\Envs\[redacted]\lib\site-packages\IPython\core\async_helpers.py in __call__(self, coro)
25 import asyncio
26
---> 27 return asyncio.get_event_loop().run_until_complete(coro)
28
29 def __str__(self):
c:\python35-64\Lib\asyncio\base_events.py in run_until_complete(self, future)
452 future.add_done_callback(_run_until_complete_cb)
453 try:
--> 454 self.run_forever()
455 except:
456 if new_task and future.done() and not future.cancelled():
c:\python35-64\Lib\asyncio\base_events.py in run_forever(self)
406 self._check_closed()
407 if self.is_running():
--> 408 raise RuntimeError('This event loop is already running')
409 if events._get_running_loop() is not None:
410 raise RuntimeError(
RuntimeError: This event loop is already running
```
然后embed(使用='asyncio')
可能会有些效果。我不知道他们为什么不给我们一个真正的解决方案
from IPython import embed
import nest_asyncio
nest_asyncio.apply()