forked from ramalho/chipy2019
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcountdown.py
More file actions
executable file
·34 lines (27 loc) · 836 Bytes
/
countdown.py
File metadata and controls
executable file
·34 lines (27 loc) · 836 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3
# Inspired by
# https://snarky.ca/how-the-heck-does-async-await-work-in-python-3-5/
import asyncio
import time
async def countdown(label, start_time, delay):
tabs = (ord(label) - ord('A')) * '\t'
n = 3
while n > 0:
await asyncio.sleep(delay)
#time.sleep(delay)
dt = time.perf_counter() - start_time
print('━' * 50)
print(f'{dt:7.2f}s \t{tabs}{label}: {n}')
n -= 1
async def main():
t0 = time.perf_counter()
tasks = [
asyncio.create_task(countdown('A', t0, .7)),
asyncio.create_task(countdown('B', t0, 2)),
asyncio.create_task(countdown('C', t0, .3)),
asyncio.create_task(countdown('D', t0, 1)),
]
await asyncio.wait(tasks)
print('━' * 50)
if __name__ == '__main__':
asyncio.run(main())