-
Notifications
You must be signed in to change notification settings - Fork 2
/
hello_world_asyncio.py
65 lines (55 loc) · 1.34 KB
/
hello_world_asyncio.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import asyncio
import time
import random
async def mock_io(io_delay):
await asyncio.sleep(io_delay)
return random.choice(["one", "two", "three", "four", "five"])
async def main():
R1 = asyncio.create_task(mock_io(5), name="R1")
R2 = asyncio.create_task(mock_io(1), name="R2")
R3 = asyncio.create_task(mock_io(3), name="R3")
# print(await R1)
# print(await R2)
# print(await R3)
print(await asyncio.gather(R1, R2, R3))
asyncio.run(main())
#
# print(end - start)
#
# async def hello():
# print("hello start")
# await asyncio.sleep(1)
# print("hello", end=" ", flush=True)
#
# async def world():
# print("world start")
# await asyncio.sleep(2)
# print("world", end=" ", flush=True)
#
# async def via():
# print("via start")
# await asyncio.sleep(3)
# print("via", end=" ", flush=True)
#
# async def async_io():
# print("asyncio start")
# await asyncio.sleep(4)
# print("asyncio", end=" ", flush=True)
#
#
# async def main():
# print("main start")
# h = asyncio.create_task(hello())
# w = asyncio.create_task(world())
# v = asyncio.create_task(via())
# a = asyncio.create_task(async_io())
#
# print("gather")
# await asyncio.gather(h,w,v,a)
# print("main end")
#
# start = time.time()
# asyncio.run(main())
# print()
# print(time.time() - start)
#