Created
May 29, 2013 04:55
-
-
Save sontek/5668049 to your computer and use it in GitHub Desktop.
Hitting URLS with a threadpool
This file contains 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
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