forked from PacktPublishing/AdvancedPythonProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample5.py
More file actions
35 lines (26 loc) · 750 Bytes
/
example5.py
File metadata and controls
35 lines (26 loc) · 750 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
35
# ch10/example5.py
from concurrent.futures import ThreadPoolExecutor
import asyncio
import time
def count_down(name, delay):
indents = (ord(name) - ord('A')) * '\t'
n = 3
while n:
time.sleep(delay)
duration = time.perf_counter() - start
print('-' * 40)
print('%.4f \t%s%s = %i' % (duration, indents, name, n))
n -= 1
async def main():
futures = [loop.run_in_executor(
executor,
count_down,
*args
) for args in [('A', 1), ('B', 0.8), ('C', 0.5)]]
await asyncio.gather(*futures)
print('-' * 40)
print('Done.')
start = time.perf_counter()
executor = ThreadPoolExecutor(max_workers=3)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())