Guest User

Untitled

a guest
Aug 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. from requests_futures.sessions import FuturesSession
  2. import os
  3. urls = []
  4. num_workers = os.cpu_count()
  5. session = FuturesSession(max_workers=num_workers)
  6. while urls:
  7. futures = []
  8. for i in range(num_workers):
  9. if not urls:
  10. break
  11. # submit a request
  12. futures.append(session.get(urls.pop(0)))
  13. # num_workers jobs have been submitted and are being worked on in the background
  14. # wait for each of them to finish
  15. for future in futures:
  16. resp = future.result()
  17. print(resp.status_code)
  18. # do something with the response
  19. # all of this batch has completed now, go around the while loop and start another
Add Comment
Please, Sign In to add comment