-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_asyncio.py
41 lines (34 loc) · 1.01 KB
/
api_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
38
39
40
import time
import requests
import asyncio
BASE_URL = "https://yukicoder.me/api/v1/"
def calc_time(fn):
def wrapper(*args, **kwargs):
start = time.time()
fn(*args, **kwargs)
end = time.time()
print(f"[{fn.__name__}] elapsed time: {end - start}")
return
return wrapper
async def get_async(path:str) -> dict:
print(f"/{path} async request")
url = BASE_URL + path
loop = asyncio.get_event_loop()
res = await loop.run_in_executor(None, requests.get, url)
print(f"/{path} async request done")
return res.json()
@calc_time
def main_async():
loop = asyncio.get_event_loop()
tasks = asyncio.gather(
get_async("problems"),
get_async("languages"),
get_async("ranking/golfer"),
get_async("statistics/tags"),
get_async("contest/future"),
)
# 非同期実行、それぞれが終わるまで
results = loop.run_until_complete(tasks)
return results
if __name__ == "__main__":
results = main_async()