task 是协程和 Future 的桥梁。
# coding=utf-8
import asyncio
import functools
def set_event (event):
print('在回调中设置事件')
event.set()
async def test (name, event):
print('\t{} waiting for event'.format(name))
await event.wait()
print('\t{} triggered'.format(name))
async def main (loop):
event = asyncio.Event()
print('事件开始状态:{}'.format(event.is_set()))
loop.call_later(0.1, functools.partial(set_event, event))
await asyncio.wait([test('e1', event), test('e2', event)])
print('事件结束状态:{}'.format(event.is_set()))
event.clear()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
try: # -------------------------------
loop.run_until_complete(main(loop))
finally:
loop.close()
输出:
事件开始状态:False
e2 waiting for event
e1 waiting for event
在回调中设置事件
e2 triggered
e1 triggered
事件结束状态:True