Created
August 27, 2016 06:23
-
-
Save 0xBADCA7/f95a3aae3b7507a61794d31cad3c3b66 to your computer and use it in GitHub Desktop.
Async HTTP requests in Python
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
from concurrent.futures import ThreadPoolExecutor | |
from requests_futures.sessions import FuturesSession | |
def outp(response): | |
print(response) | |
print(response.headers) | |
print(response.text) | |
urls = [ | |
"https://www.google.com", | |
"http://github.com", | |
"http://www.microsoft.com" | |
] | |
responses = [] | |
session = FuturesSession(executor=ThreadPoolExecutor(max_workers=10)) | |
for url in urls: | |
future = session.get(url) | |
response = future.result() | |
responses.append(response) | |
id = 0 | |
for response in responses: | |
id += 1 | |
print(u"{}: {}\r\n".format(id, response.content)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment