协程是否正在等待asyncio.Event设置为保证每次设置事件时都会得到通知?
Event.set()和Event.clear()都不是可等待的。因此,可以设置和清除事件,而无需将控制返回到循环。因此,我猜调用set()和clear()不会将事件通知侍者。我发现确保通知服务员的唯一方法是添加asycnio.sleep(0),以将控件强制返回到set()和clear()之间的循环。但这对我来说看起来很丑。
这里是一个例子:
NUM_EVENTS = 1000
NUM_WAITERS = 1000
import asyncio
async def waiter(event,id):
print('waiting for it ...')
for i in range(NUM_EVENTS):
await event.wait()
await asyncio.sleep(0) # might be dropped by affects order
print(f'... {id} got {i}')
async def main():
# Create an Event object.
event = asyncio.Event()
# Spawn a Task to wait until 'event' is set.
waiter_tasks = []
for i in range(NUM_WAITERS):
waiter_tasks.append(asyncio.create_task(waiter(event,i)))
# Sleep for 1 second and set the event.
await asyncio.sleep(1)
for i in range(NUM_EVENTS):
event.set()
await asyncio.sleep(0) # leaving this out will result in the waiters only receiving the event once
event.clear()
# Wait until the waiter task is finished.
await asyncio.wait(waiter_tasks)
if __name__ == '__main__':
asyncio.run(main())
在第31行上省略asyncio.sleep(0)
会使服务员仅接收一次(而不是1000次)事件。
但是如何干净地做到这一点?