Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
oorrja
GitHub Repository: oorrja/learntosolveit
Path: blob/master/languages/python/asyncio_examples/stopping_loop.py
1240 views
1
import asyncio
2
3
async def say(what, when):
4
await asyncio.sleep(when)
5
print(what)
6
7
async def stop_after(loop, when):
8
await asyncio.sleep(when)
9
loop.stop()
10
11
loop = asyncio.get_event_loop()
12
13
loop.create_task(say("first hello", 2))
14
loop.create_task(say("second hello", 1))
15
loop.create_task(say("third hello", 4))
16
loop.create_task(stop_after(loop, 3))
17
18
loop.run_forever()
19
loop.close()
20
21