我主要在 IPython 的交互式 shell 中工作。有时我会从 Internet 上复制粘贴代码来测试它并使用示例。
如果我从 this tutorial 粘贴以下代码:
import asyncio
async def speak_async():
print('OMG asynchronicity!')
loop = asyncio.get_event_loop()
loop.run_until_complete(speak_async())
loop.close()
我将关闭循环。 documentation说在事件循环关闭后不要在事件循环上使用任何方法。 async.get_event_loop()
仍将返回该闭环。
如果我不小心关闭了一个事件循环,我该怎么办?我宁愿不重启解释器。
最佳答案
你可以 create and set a new event loop for the current context ;
asyncio.set_event_loop(asyncio.new_event_loop())
关于python - 如何替换关闭的事件循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35021287/