Skip to content

Instantly share code, notes, and snippets.

@kirang89
Last active December 13, 2015 17:28
Show Gist options
  • Save kirang89/4948032 to your computer and use it in GitHub Desktop.
Save kirang89/4948032 to your computer and use it in GitHub Desktop.
#
# 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