-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathex11.py
More file actions
41 lines (31 loc) · 933 Bytes
/
ex11.py
File metadata and controls
41 lines (31 loc) · 933 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
36
37
38
39
40
41
import asyncio
import concurrent.futures
from time import time
def blocks(n):
counter = n
start = time()
while counter > 0:
counter -= 1
return time() - start
async def monitoring():
while True:
await asyncio.sleep(2)
print(f"Monitoring {time()}")
async def run_blocking_tasks(executor, n):
loop = asyncio.get_event_loop()
print("waiting for executor tasks")
result = await loop.run_in_executor(executor, blocks, n)
return result
async def main():
asyncio.create_task(monitoring())
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
futures = [
run_blocking_tasks(executor, n)
for n in [50_000_000, 60_000_000, 70_000_000]
]
results = await asyncio.gather(*futures)
return results
if __name__ == "__main__":
result = asyncio.run(main())
for r in result:
print(r)