python3 asyncio 是否使用像 Rust Tokio 这样的工作窃取调度程序?

Does python3 asyncio use a work stealing scheduler like Rust Tokio?

Python3 asyncio 是否使用像 Rust Tokio 这样的工作窃取调度程序?默认调度程序的行为是什么?它在某处记录了吗?

"Work-stealing" is a property of multi-threaded executors. Python asyncio's executor (event loop) is single-threaded, so it's by definition not work-stealing. The behavior of the asyncio event loop wrt threads is documented (among other places) in the Concurrency and Multithreading 文档部分。

至于用于调度的算法,有意未指定,但stdlib implementation使用:

  • 一个双端队列,用于存储准备好 运行 的回调(使用 call_soon()create_task() 安排的回调)以及与准备好 [=31] 的文件描述符关联的回调=],以及
  • 一个二进制堆,用于存储为特定时间安排的回调,按照它们应该触发的绝对时间排序。这包括由 loop.call_after()loop.call_at() 安排的回调,也包括由 asyncio.sleep() 暂停的协程的延续,它在内部使用 loop.call_at().

在每次循环迭代时,循环 waits for something to happen on file descriptors associated with coroutines and sets the timeout 在最近的基于时间的回调中中断睡眠,以防在此之前没有发生任何有趣的事情。它继续在当前或更早的时间调用就绪回调和计划到 运行 的超时。重复此操作,直到指示事件循环停止。