Skip to content

Instantly share code, notes, and snippets.

@circa10a
Last active August 20, 2018 17:34
Show Gist options
  • Save circa10a/7a6d8bd2ee4476c8e2c9b8799bf5728b to your computer and use it in GitHub Desktop.
Save circa10a/7a6d8bd2ee4476c8e2c9b8799bf5728b to your computer and use it in GitHub Desktop.
request_futures sample usage
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