forked from calebmadrigal/asyncio-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_in_executor.py
32 lines (23 loc) · 881 Bytes
/
run_in_executor.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
import asyncio
from urllib.request import urlopen
async def print_data_size():
data = await get_data_size()
print(f"Data size: {data['data1']} + {data['data2']} = { data['data1'] + data['data2'] }")
# Note that this is a synchronous function
def sync_get_url(url):
return urlopen(url).read()
async def get_data_size():
loop = asyncio.get_event_loop()
# These each run in their own thread (in parallel)
future1 = loop.run_in_executor(None, sync_get_url, 'http://xkcd.com')
future2 = loop.run_in_executor(None, sync_get_url, 'http://google.com')
# While the synchronous code above is running in other threads, the event loop
# can go do other things.
data1 = await future1
data2 = await future2
return {
"data1": len(data1),
"data2": len(data2),
}
loop = asyncio.get_event_loop()
loop.run_until_complete(print_data_size())