Last active
December 13, 2015 17:28
-
-
Save kirang89/4948032 to your computer and use it in GitHub Desktop.
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
# | |
# Testing multithreaded operations using concurrent.futures. | |
# | |
# The concurrent.futures library was introduced in Python 3.2, but if you want to run it in 2.x, | |
# you can download the backport of this library from http://pypi.python.org/pypi/futures | |
# | |
from concurrent.futures import ThreadPoolExecutor | |
import requests | |
def ping_url(url, worker): | |
try: | |
res = requests.head(url) | |
except e: | |
res = e | |
print "Completed %s " % worker | |
with ThreadPoolExecutor(max_workers = 4) as executor: | |
url = "http://httpbin.org/delay/2" | |
for worker in range(10): | |
executor.submit(ping_url, url, worker) | |
executor.shutdown() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment