Last active
August 20, 2018 17:34
-
-
Save circa10a/7a6d8bd2ee4476c8e2c9b8799bf5728b to your computer and use it in GitHub Desktop.
request_futures sample usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from requests_futures.sessions import FuturesSession | |
import os | |
urls = [] | |
num_workers = os.cpu_count() | |
session = FuturesSession(max_workers=num_workers) | |
while urls: | |
futures = [] | |
for i in range(num_workers): | |
if not urls: | |
break | |
# submit a request | |
futures.append(session.get(urls.pop(0))) | |
# num_workers jobs have been submitted and are being worked on in the background | |
# wait for each of them to finish | |
for future in futures: | |
resp = future.result() | |
print(resp.status_code) | |
# do something with the response | |
# all of this batch has completed now, go around the while loop and start another |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment