Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
oorrja
GitHub Repository: oorrja/learntosolveit
Path: blob/master/languages/python/asyncio_examples/hello_clock.py
1240 views
1
"""
2
Hello Clock
3
4
Example illustrating how to schedule two coroutines to run concurrently.
5
They run for ten minutes, during which the first coroutine is scheduled to run every second, while the second is scheduled to run every minute.
6
"""
7
8
import asyncio
9
10
async def print_every_second():
11
""""Print seconds."""
12
while True:
13
for i in range(60):
14
print((i, 's'))
15
await asyncio.sleep(1)
16
17
async def print_every_minute():
18
"""Print Every minute."""
19
for i in range(1, 10):
20
await asyncio.sleep(60)
21
print((i, "minute"))
22
23
loop = asyncio.get_event_loop()
24
loop.run_until_complete(
25
asyncio.gather(print_every_second(), print_every_minute()))
26
loop.close()
27
28