-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry_asyncio.py
37 lines (25 loc) · 886 Bytes
/
try_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
import asyncio
async def print_hello():
print("hello")
async def print_world():
await asyncio.sleep(1)
print("world")
async def print_from():
await asyncio.sleep(2)
print("from")
async def print_mandar():
await asyncio.sleep(3)
print("Mandar")
async def main():
task_list = list()
task_list.append(asyncio.create_task(print_mandar()))
task_list.append(asyncio.create_task(print_from()))
task_list.append(asyncio.create_task(print_world()))
task_list.append(asyncio.create_task(print_hello()))
# prints "hello world from Mandar", regardless of the sequence in list
for item in task_list:
await item
if __name__ == '__main__':
# asyncio.run can be used only with Python3.7
# asyncio.run(main())
asyncio.get_event_loop().run_until_complete(asyncio.gather(main()))