解决 Python 报错 RuntimeError: Event loop is closed 退出异常
分析 Python 异步程序退出时底层套接字垃圾回收未完成报 Event loop is closed 根因,提供 Windows 策略与优雅排空方案。
问题现象
脚本正常结束时抛出:RuntimeError: Event loop is closed。
触发根因
Proactor 传输层析构晚于事件循环关闭。
解决步骤与操作命令
步骤一:切换 Windows 事件循环策略
import sys, asyncio
if sys.platform.startswith('win'):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
退出清爽无报错。


