我正在学习用于异步编程的Python asyncio . 我知道事件循环监视Future对象,直到它们准备就绪,然后恢复相应的协同程序以继续执行await关键字发生的位置 .
当你使用像 asyncio.sleep
这样的东西时,这是非常容易理解的,因为睡眠函数知道需要多少时间,所以知道事件循环但是 what happens with something that relies on networking ( for example) where the waiting time is unknown? .
事件循环如何知道资源何时准备好或者从某个源收集数据需要多少时间?
1 回答
默认事件循环(基于SelectorEventLoop)使用selector模块跟踪要监视的所有资源,并在新数据准备好时收到通知 . BaseSelector.select是where the magic happens .