python - futures.ThreadPoolExecutor Exceptions -
i'm implementing futures.threadpoolexecutor , change broad exception specific exceptions (e.g. valueerror). using example below should add exceptions generated load_url instead of broad exception? exceptions section of code referring to.
using this page reference:
import concurrent.futures import urllib.request urls = ['http://www.foxnews.com/', 'http://www.cnn.com/', 'http://europe.wsj.com/', 'http://www.bbc.co.uk/', 'http://some-made-up-domain.com/'] # retrieve single page , report url , contents def load_url(url, timeout): if not url: raise valueerror('invalid url') urllib.request.urlopen(url, timeout=timeout) conn: return conn.read() # can use statement ensure threads cleaned promptly concurrent.futures.threadpoolexecutor(max_workers=5) executor: # start load operations , mark each future url future_to_url = {executor.submit(load_url, url, 60): url url in urls} future in concurrent.futures.as_completed(future_to_url): url = future_to_url[future] try: data = future.result() except exception exc: print('%r generated exception: %s' % (url, exc)) else: print('%r page %d bytes' % (url, len(data)))
Comments
Post a Comment