[Python-Dev] cpython (3.3): Tweak the threaded example in concurrent.futures

Antoine Pitrou solipsis at pitrou.net
Tue Oct 16 14:58:18 CEST 2012


On Tue, 16 Oct 2012 14:51:21 +0200 (CEST)
nick.coghlan <python-checkins at python.org> wrote:
>  
> +   # We can use a with statement to ensure threads are cleaned up promptly
>     with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
> -       future_to_url = dict((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]
> -           if future.exception() is not None:
> -               print('%r generated an exception: %s' % (url,
> -                                                        future.exception()))
> +       # Start the load operations and mark each future with its URL
> +       load_urls = [executor.submit(load_url, url, 60) for url in URLS]
> +       for future, url in zip(load_urls, URLS):
> +           future.url = url

Adding an "url" attribute here looks a bit ugly to me. Why not use a
dict comprehension for future_to_url?

Regards

Antoine.


-- 
Software development and contracting: http://pro.pitrou.net




More information about the Python-Dev mailing list