-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathasynchronous _101.py
48 lines (32 loc) · 1.14 KB
/
asynchronous _101.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
import asyncio
import time
# Section 1
async def sum(x1: int, x2: int) -> None:
print(f"Sum of {x1} and {x2} is calculating...")
await asyncio.sleep(1)
print(f"Sum of {x1} and {x2} is {x1+x2}")
# calling a async function -> running a coroutine way 1
asyncio.run(sum(x1=14, x2=16))
# Section 2
async def say_message_after(delay: int, message: str) -> None:
await asyncio.sleep(delay)
print(message)
async def call_say_hello():
print(f"started at {time.strftime('%X')}")
# calling a async function -> running a coroutine way 2
await say_message_after(1, "hello")
await say_message_after(1, "hola")
print(f"finished at {time.strftime('%X')}")
# call call_say_hello
asyncio.run(call_say_hello())
async def call_say_hello():
t1 = asyncio.create_task(say_message_after(1, "merhaba"))
t2 = asyncio.create_task(say_message_after(1, "Привет"))
print(f"started at {time.strftime('%X')}")
# Wait until both tasks are completed (should take
# around 2 seconds.)
await t1
await t2
print(f"finished at {time.strftime('%X')}")
if __name__ == "__main__":
asyncio.run(call_say_hello())