We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 65a59e0 commit b607be5Copy full SHA for b607be5
examples/asyncio-prompt.py
@@ -31,10 +31,7 @@ async def print_counter():
31
while True:
32
print('Counter: %i' % i)
33
i += 1
34
- try:
35
- await asyncio.sleep(3)
36
- except asyncio.CancelledError:
37
- return
+ await asyncio.sleep(3)
38
39
40
async def interactive_shell():
@@ -64,12 +61,14 @@ async def interactive_shell():
64
61
65
62
66
63
def main():
67
- counter = loop.create_task(print_counter())
68
shell = loop.create_task(interactive_shell())
+
+ # This gathers all the async calls, so they can be cancelled at once
+ tasks = asyncio.gather(print_counter(), return_exceptions=True)
69
70
loop.run_until_complete(shell)
71
- counter.cancel()
72
- loop.run_until_complete(counter)
+ tasks.cancel()
+ loop.run_until_complete(tasks)
73
print('Qutting event loop. Bye.')
74
loop.close()
75
0 commit comments