-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20230618_async_sample.py
More file actions
executable file
·39 lines (31 loc) · 1.15 KB
/
20230618_async_sample.py
File metadata and controls
executable file
·39 lines (31 loc) · 1.15 KB
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
import asyncio
import requests
import timeit
import threading
def _invoke_api():
print('thread id:', threading.get_ident())
url = 'https://data.taipei/api/v1/dataset/cba4734b-3693-46cc-bcc1-61cac76386b9?scope=resourceAquire'
response = requests.get(url, json={
'resource_id': 'cba4734b-3693-46cc-bcc1-61cac76386b9'
}, verify=False)
print(response.json()['result']['results'][0])
async def sub_function(index: int, loop:asyncio.AbstractEventLoop):
print(index, 'entry sub function')
await asyncio.to_thread(_invoke_api)
# future = loop.run_in_executor(None, _invoke_api)
# await asyncio.wait_for(future, timeout=3, loop=loop)
print('leave sub function')
async def main():
loop = asyncio.get_event_loop()
# task = loop.create_task(_invoke_api())
# await asyncio.gather(_invoke_api())
task = []
# for i in range(0, 5):
# task.append(asyncio.create_task(sub_function(i)))
task.append(sub_function(1, loop))
await asyncio.gather(*task)
if __name__ == '__main__':
start = timeit.default_timer()
asyncio.run(main())
end = timeit.default_timer()
print(start, end, end-start)