Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- asyncio.wait([a sequence of futures])
- asyncio.wait_for(a single future)
- import asyncio
- from itertools import count
- async def counter1():
- cnt = count(1)
- try:
- while True:
- print('From 1:', next(cnt))
- await asyncio.sleep(1)
- except asyncio.CancelledError:
- print('counter1 cancelled')
- async def counter2():
- cnt = count(1)
- for i in range(5):
- print('From 2:', next(cnt))
- await asyncio.sleep(1)
- cnt1.cancel()
- # await asyncio.wait([cnt1]) # works
- # await asyncio.wait_for(cnt1) # hangs
- await asyncio.wait_for(cnt1, 1) # works
- loop.stop()
- loop = asyncio.get_event_loop()
- cnt1 = asyncio.ensure_future(counter1())
- cnt2 = asyncio.ensure_future(counter2())
- loop.run_forever()
Add Comment
Please, Sign In to add comment