forked from PacktPublishing/AdvancedPythonProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample2.py
More file actions
30 lines (22 loc) · 619 Bytes
/
example2.py
File metadata and controls
30 lines (22 loc) · 619 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
# ch10/example2.py
import asyncio
import time
async def count_down(name, delay):
indents = (ord(name) - ord('A')) * '\t'
n = 3
while n:
await asyncio.sleep(delay)
duration = time.perf_counter() - start
print('-' * 40)
print('%.4f \t%s%s = %i' % (duration, indents, name, n))
n -= 1
loop = asyncio.get_event_loop()
tasks = [
loop.create_task(count_down('A', 1)),
loop.create_task(count_down('B', 0.8)),
loop.create_task(count_down('C', 0.5))
]
start = time.perf_counter()
loop.run_until_complete(asyncio.wait(tasks))
print('-' * 40)
print('Done.')