Skip to content

Instantly share code, notes, and snippets.

@goutomroy
Last active July 21, 2019 21:49
Show Gist options
  • Save goutomroy/0849597eef292d5b9eec83676b660280 to your computer and use it in GitHub Desktop.
Save goutomroy/0849597eef292d5b9eec83676b660280 to your computer and use it in GitHub Desktop.
import concurrent.futures
import urllib.request
from time import sleep
URLS = ['http://www.foxnews.com/',
'http://www.cnn.com/',
'http://europe.wsj.com/',
'http://www.bbc.co.uk/',
'http://some-made-up-domain.com/']
def load_url(url, timeout):
with urllib.request.urlopen(url, timeout=timeout) as conn:
if url == 'http://www.cnn.com/':
sleep(10)
return conn.read()
if __name__ == "__main__":
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}
for future in concurrent.futures.as_completed(future_to_url):
url = future_to_url[future]
try:
data = future.result()
except Exception as exc:
print(f'{url} generated an exception: {exc}\n')
else:
print(f'{url} page size is {len(data)} bytes')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment