-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasyncio_examples.py
More file actions
49 lines (30 loc) · 936 Bytes
/
asyncio_examples.py
File metadata and controls
49 lines (30 loc) · 936 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
44
45
46
47
48
49
import asyncio
import random
async def add(a, b):
return a + b
# add(1, 1) # <coroutine object add at 0x77344e554e80>
asyncio.run(add(1, 1))
async def db_get():
await asyncio.sleep(0.5)
return random.randint(1,10)
asyncio.run(db_get())
async def process_data():
# Najpierw trzeba poczekać na pierwszy wynik, potem na drugi:
a = await db_get()
b = await db_get()
return a * b
asyncio.run(process_data())
async def process_data_faster():
# Czekanie na dwa db_get odbywa się równolegle
a, b = await asyncio.gather(db_get(), db_get())
return a * b
asyncio.run(process_data_faster())
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(asyncio.gather(db_get(), db_get()))
def some_function_with_callback(callback):
retult = 123 # result of some calculation
if asyncio.iscoroutinefunction(callback):
asyncio.run(callback(retult))
else:
callback(retult)