平臺支持?

asyncio 模塊被設(shè)計為可移植的,但由于平臺的底層架構(gòu)和功能,一些平臺存在細微的差異和限制。

所有平臺?

Windows?

Windows上的所有事件循環(huán)都不支持以下方法:

SelectorEventLoop 有下列限制:

ProactorEventLoop 有下列限制:

Windows上單調(diào)時鐘的分辨率大約為 15.6 毫秒。最佳的分辨率是 0.5 毫秒。分辨率依賴于具體的硬件(HPET)和Windows的設(shè)置。

Windows的子進程支持?

Windows上的 SelectorEventLoop 不支持子進程,使用 ProactorEventLoop 代替它:

import asyncio

asyncio.set_event_loop_policy(
    asyncio.WindowsProactorEventLoopPolicy())

asyncio.run(your_code())

也不支持 policy.set_child_watcher() 函數(shù),ProactorEventLoop 有不同的機制來監(jiān)視子進程。

macOS?

完整支持流行的macOS版本。

macOS <= 10.8

在 macOS 10.6, 10.7 和 10.8 上,默認的事件循環(huán)使用 selectors.KqueueSelector,在這些版本上它并不支持字符設(shè)備。 可以手工配置 SelectorEventLoop 來使用 SelectSelectorPollSelector 以在這些較老版本的 macOS 上支持字符設(shè)備。 例如:

import asyncio
import selectors

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