-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_await.py
More file actions
43 lines (31 loc) · 887 Bytes
/
Copy pathasync_await.py
File metadata and controls
43 lines (31 loc) · 887 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
42
43
'''import asyncio
import time
async def my_func():
start = time.time()
sleep_func = asyncio.sleep(2)
print(time.time() - start)
await sleep_func
print(time.time() - start)
asyncio.run(my_func)()'''
import asyncio
import random
import time
async def task(name):
await asyncio.sleep(random.randint(1,3))
return f"{name} finished"
async def main():
tasks = [task(f"Task {i}") for i in range(1,4)]
start_time = time.time()
# for coro in asyncio.as_completed(tasks):
# result = await coro
# end_time = time.time()
# print(end_time - start_time, result)
results = await asyncio.gather(*tasks)
end_time = time.time()
print(f"gather look {end_time - start_time}")
start_printing = time.time()
for result in results:
print(time.time() - start_printing, results)
print(f"Total time {time.time() - start_time}")
if __name__ == '__main__':
asyncio.run(main())