asyncio.wait订单有保障吗?
Is asyncio.wait order guaranteed?
我正在尝试在基于 asyncio 的库中实现公平排队。
在某些函数中,我有这样的语句(假设 socketX
是任务):
done, pending = asyncio.wait(
[socket1, socket2, socket3],
return_when=asyncio.FIRST_COMPLETED,
)
现在我多次阅读 asyncio.wait
的文档,但它不包含我想要的信息。主要是,我想知道是否:
当我发出呼叫时,socket1
、socket2
和 socket3
恰好已经准备就绪。是否保证 done
将全部包含它们,还是 returns 只有一个(或两个)?
- 在第二种情况下,传递给
wait()
的任务顺序重要吗?
我正在尝试断言我是否可以在 done
任务集中应用公平排队(通过选择一个任务并将其他任务留待以后解决)或者我是否还需要关心我传递任务的顺序。
文档对此有点沉默。有什么想法吗?
这只是根据Python3.5的source code取的。
如果future在调用wait
之前完成,它们都会被放在done
集合中:
import asyncio
async def f(n):
return n
async def main():
(done, pending) = await asyncio.wait([f(1), f(2), f(3)], return_when=asyncio.FIRST_COMPLETED)
print(done) # prints set of 3 futures
print(pending) # prints empty set
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
我正在尝试在基于 asyncio 的库中实现公平排队。
在某些函数中,我有这样的语句(假设 socketX
是任务):
done, pending = asyncio.wait(
[socket1, socket2, socket3],
return_when=asyncio.FIRST_COMPLETED,
)
现在我多次阅读 asyncio.wait
的文档,但它不包含我想要的信息。主要是,我想知道是否:
-
当我发出呼叫时,
socket1
、socket2
和socket3
恰好已经准备就绪。是否保证done
将全部包含它们,还是 returns 只有一个(或两个)?- 在第二种情况下,传递给
wait()
的任务顺序重要吗?
我正在尝试断言我是否可以在 done
任务集中应用公平排队(通过选择一个任务并将其他任务留待以后解决)或者我是否还需要关心我传递任务的顺序。
文档对此有点沉默。有什么想法吗?
这只是根据Python3.5的source code取的。
如果future在调用wait
之前完成,它们都会被放在done
集合中:
import asyncio
async def f(n):
return n
async def main():
(done, pending) = await asyncio.wait([f(1), f(2), f(3)], return_when=asyncio.FIRST_COMPLETED)
print(done) # prints set of 3 futures
print(pending) # prints empty set
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()