Skip to content

Instantly share code, notes, and snippets.

@sontek
Created May 29, 2013 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sontek/5668049 to your computer and use it in GitHub Desktop.
Save sontek/5668049 to your computer and use it in GitHub Desktop.
Hitting URLS with a threadpool
import threading
import requests
import logging
from threadpool import ThreadPool
from threadpool import makeRequests
pool = ThreadPool(4)
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
http = requests.Session()
def fetch(url):
current_thread = threading.current_thread()
print("IN THREAD %s" % current_thread.name)
result = http.get(url)
print("Found status %s, content_length %s" % (
result.status_code
, len(result.content)
)
)
urls = ["http://sontek.net"] * 10
requests = makeRequests(fetch, urls)
for req in requests:
pool.putRequest(req)
pool.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment