平臺支援

asyncio 模組被設計為可移植的,但由於平臺底層架構和功能的限制,一些平臺存在細微的差異和限制。

所有平臺

Windows

原始碼: Lib/asyncio/proactor_events.py, Lib/asyncio/windows_events.py, Lib/asyncio/windows_utils.py


在 3.8 版本中更改: 在 Windows 上,ProactorEventLoop 現在是預設的事件迴圈。

Windows 上的所有事件迴圈都不支援以下方法

SelectorEventLoop 有以下限制

ProactorEventLoop 有以下限制

Windows 上單調時鐘的解析度通常約為 15.6 毫秒。最佳解析度為 0.5 毫秒。解析度取決於硬體(HPET 的可用性)和 Windows 配置。

Windows 上的子程序支援

在 Windows 上,預設的事件迴圈 ProactorEventLoop 支援子程序,而 SelectorEventLoop 不支援。

也不支援 policy.set_child_watcher() 函式,因為 ProactorEventLoop 有不同的機制來監視子程序。

macOS

完全支援現代 macOS 版本。

macOS <= 10.8

在 macOS 10.6、10.7 和 10.8 上,預設的事件迴圈使用 selectors.KqueueSelector,在這些版本上不支援字元裝置。可以手動配置 SelectorEventLoop 以使用 SelectSelectorPollSelector 以在這些較舊的 macOS 版本上支援字元裝置。 示例

import asyncio
import selectors

selector = selectors.SelectSelector()
loop = asyncio.SelectorEventLoop(selector)
asyncio.set_event_loop(loop)