Posted at

Pythonのrequests_futureの実行方法による速度比較

Pythonのrequests_futuresの実行方法によって速度が変わる。

requests_futureを使うときは、sessionを一度にまとめて作って、後から評価するようにしないと並列で動かない。多分

テストコード

from concurrent.futures import ThreadPoolExecutor

from requests_futures.sessions import FuturesSession
import requests
import time

session = FuturesSession(executor=ThreadPoolExecutor(max_workers=30))

num_links = 100
links = [f"http://example.com/{_}" for _ in range(1,num_links)]

fs = []
print(f"requesting to {num_links} links")
t1 = time.time()
for l in links:
f = session.get(l)
fs.append(f)
t2 = time.time()
print(f"push to pool w/ requests_future: {t2-t1} sec")

for f in fs:
f.result().status_code == 200
t3 =time.time()
print(f"eval in pool w/ requests_future: {t3-t2} sec")

for l in links:
f = session.get(l)
f.result().status_code == 200
t4 = time.time()
print(f"eval everytime w/ requests_future: {t4-t3} sec")

for l in links:
r = requests.get(l)
r.status_code == 200
t5 = time.time()
print(f"eval everytime w/ requests: {t5-t5} sec")

実行結果

requesting to 100 links

push to pool w/ requests_future: 0.08985710144042969 sec
eval in pool w/ requests_future: 0.9865047931671143 sec
eval everytime w/ requests_future: 24.10741138458252 sec
eval everytime w/ requests: 44.42911386489868 sec