title: How to get the currentThread().getName() as part of (concurrent.futures) future result?
body: <p>I have the following code with questions at the bottom:</p>
  <pre><code>1  from threading import currentThread
  2  import concurrent.futures
  3  import urllib.request
  4 
  5  URLS = ['http://www.foxnews.com/',
  6          'http://www.cnn.com/',
  7          'http://www.bbc.co.uk/']
  8 
  9 
  10
  11 # Retrieve a single page and report the URL and contents
  12 def load_url(url, timeout):
  13    with urllib.request.urlopen(url, timeout=timeout) as conn:
  14
  15        print(currentThread().getName(), url)
  16        # how do I pass back the thread_name with the &quot;conn.read&quot; back to executor.submit? 
  17        return conn.read()
  18
  19
  20 # We can use a with statement to ensure threads are cleaned up promptly
  21 with concurrent.futures.ThreadPoolExecutor(max_workers=3, thread_name_prefix='url_thread') as executor:
  22    # Start the load operations and mark each future with its URL
  23    futures_dict = {executor.submit(load_url, url, 60): url for url in URLS}
  24    for future in concurrent.futures.as_completed(futures_dict):
  25        url = futures_dict[future]
  26        try:
  27            data = future.result()
  28        except Exception as exc:
  29            # print('%r generated an exception: %s' % (url, exc))
  30            print(f'Thread Name {currentThread().getName()}: {url} generated an exception {exc}')
  31        else:
  32            # print('%r page is %d bytes' % (url, len(data)))
  33            print(f'Thread Name {currentThread().getName()}: content length of {url} = {len(data)} bytes')
  
  </code></pre>
  <p>My output is as follows:</p>
  <pre><code>url_thread_1 http://www.cnn.com/
  url_thread_0 http://www.foxnews.com/
  url_thread_2 http://www.bbc.co.uk/
  Thread Name MainThread: content length of http://www.foxnews.com/ = 282968 bytes
  Thread Name MainThread: content length of http://www.cnn.com/ = 1115357 bytes
  Thread Name MainThread: content length of http://www.bbc.co.uk/ = 363642 bytes
  </code></pre>
  <p>Questions:</p>
  <p>The above shows the executor.submit target function <code>load_url</code> returns <code>conn.read()</code> (line 17).</p>
  <p>If I do <code>print(threading.currentThread.getName())</code> within the <code>with ThreadPoolExecutor</code> statement (lines 30 and 33), it always shows &quot;MainThread&quot;.</p>
  <p>But when I do <code>print(threading.currentThread.getName())</code> within the target function <code>load_url</code> (see line 15) - it correctly shows <code>url_thread_0</code>, <code>url_thread_1</code>, etc.
  (please note that I am passing <code>max_workers=3</code> and <code>thread_name_prefix='url_thread</code> as parameters to the ThreadPoolExecutor)</p>
  <p>How do I also pass along the <code>currentThread().getName()</code>  from line 15 along with the <code>conn.read()</code> from line 17 as as part of the futures_dict key, so I can use it to display the correct thread name in either line 30 or line 33?</p>