异步事件循环如何知道等待资源何时准备就绪?

How does asyncio's event loop know when an awaitable resource is ready?

我正在学习 Python 用于异步编程的 asyncio。我知道事件循环会监视 Future 对象,直到它们准备就绪,然后恢复适当的协程以在 await 关键字出现的位置继续执行。

当你使用像 asyncio.sleep 这样的东西时,这是非常可以理解的,因为休眠函数知道它需要多少时间,所以会知道事件循环,但是 依赖于它的东西会发生什么等待时间未知的网络(例如)?

事件循环如何知道资源何时准备就绪或从某个源收集数据需要多少时间?

How does the event loop know when a resource is ready or how many time will take to gather data from some source?

默认事件循环(基于SelectorEventLoop) uses the selector module to keep track of all the resources to monitor and get notified when new data is ready. BaseSelector.select is where the magic happens