代码:
loop = asyncio.get_event_loop()
# add tasks to the loop
# ...
# and then run the loop
try:
loop.run_forever()
except KeyboardInterrupt:
print(loop)
# Here I need to run a cleanup, I still need to use the event loop
# Can I still use the event loop here? like:
loop.run_until_complete(some_cleanup_coro())
当我在except块中打印事件循环时,我看到输出:WindowsSelectorEventLoop, with closed=False, 这running=False. 是否意味着我不能在except块中使用事件循环?那么我该如何运行清理协程呢?
该run_until_complete呼叫挂起并没有运行。所以按下ctrl+c不会退出,我必须关闭终端本身。
loop.close()和之间有什么区别loop.stop(),我应该叫它们吗?文档没有提及loop.stop().
我的cleanup_coro()主要asyncio.open_connection(..)是发送和接收一条消息。(据我所知,根本没有发送消息)。
慕容森
随时随地看视频慕课网APP
相关分类