ProactorEventLoop自Python 3.8起成为Windows默认事件循环,旨在利用IOCP提升高并发I/O性能,但因兼容性差(如tornado、jupyter不支持)和CVE-2026-3298越界写入漏洞,常需回退至WindowsSelectorEventLoopPolicy。

ProactorEventLoop 成为 Windows 默认,不是因为“更先进”或“更推荐”,而是 Python 官方在 3.8 版本中主动切换了默认策略,目的是利用 Windows 原生的 IOCP(I/O Completion Port)机制提升高并发网络 I/O 性能。但这个切换带来了实际兼容性代价。
ProactorEventLoop 在 Windows 上触发 ValueError: too many file descriptors in select()
这个错误根本不是你代码写的有问题,而是事件循环用错了。SelectorEventLoop 在 Windows 上底层调用的是模拟的 select(),硬性限制 512 个句柄(socket、pipe、临时文件等全算)。一旦并发连接数超限(比如用 aiohttp 发 1000 个请求),立刻崩。
ProactorEventLoop 不走 select(),它基于 IOCP,理论支持数万连接——所以 Python 3.8 把它设为默认,是为了解决这个瓶颈。
但问题在于:很多库没适配它。比如 tornado、jupyter、早期 nest_asyncio,都依赖 SelectorEventLoop 的行为(尤其是对子进程、stdin/stdout、信号处理的支持)。
立即学习“Python免费学习笔记(深入)”;
Nodriver是Undetected-Chromedriver的官方继承者,提供异步浏览器自动化功能,可直接与Chrome DevTools Protocol通信,无需Selenium或WebDriver依赖。专为规避反机器人系统(包括Cloudflare和Imperva)的隐蔽网络自动化而构建。
-
tornado在 Windows 上直接抛NotImplementedError,因为它检测到当前 loop 不是WindowsSelectorEventLoopPolicy -
jupyter启动失败,报错源头是tornado.platform.asyncio拒绝初始化 - 你手动调
asyncio.run()却卡住不动?很可能是ProactorEventLoop在初始化 IOCP 时和某些子进程/控制台操作冲突
如何强制回退到 WindowsSelectorEventLoopPolicy
最稳妥的做法,是在整个程序入口第一行就设置策略(早于任何 import tornado 或 import jupyter):
import asyncio asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
- 这个调用必须在
asyncio.get_event_loop()或asyncio.run()之前执行,否则无效 - 如果你用的是第三方包(如
jupyter),无法改它的启动脚本,就得去它安装目录里手动 patch:tornado/platform/asyncio.py,在import asyncio后加同一行 - 注意:不要在子线程里调
set_event_loop_policy(),它只影响当前进程的主线程策略
ProactorEventLoop 还有个高危安全风险:CVE-2026-3298
这个漏洞仅存在于 ProactorEventLoop.sock_recvfrom_into() 中,当传入 nbytes 参数且网络数据超过缓冲区大小时,会触发越界写入(OOB write),导致内存损坏甚至远程代码执行。
- 影响所有 Python 3.8+ 的 Windows 用户
- 只要你的服务用 UDP +
sock_recvfrom_into(..., nbytes=...),又跑在 Windows 上,就可能中招 - Linux/macOS 完全不受影响(它们用
SelectorEventLoop)
补丁还没全民推送,所以目前最实际的防御动作是:除非你明确需要 IOCP 高并发能力,否则别碰 ProactorEventLoop
Windows 上 asyncio 的默认策略从来就不是“选最优”,而是在性能、兼容、安全之间反复权衡的结果。现在它默认开 ProactorEventLoop,但你几乎每次遇到问题,都要关掉它。

















