]> granicus.if.org Git - python/commitdiff
bpo-29212: Fix the ugly repr() ThreadPoolExecutor thread name. (#2315)
authorGregory P. Smith <greg@krypto.org>
Thu, 22 Jun 2017 06:41:13 +0000 (23:41 -0700)
committerGitHub <noreply@github.com>
Thu, 22 Jun 2017 06:41:13 +0000 (23:41 -0700)
bpo-29212: Fix the ugly ThreadPoolExecutor thread name.

Fixes the newly introduced ugly default thread name for concurrent.futures
thread.ThreadPoolExecutor threads.  They'll now resemble the old <=3.5
threading default Thread-x names by being named ThreadPoolExecutor-y_n.

Lib/concurrent/futures/thread.py
Lib/test/test_concurrent_futures.py
Misc/NEWS

index 03d276b63f63cae1cf711a4cb2236ed06c7b9b0a..1f0a1d4b977f316cdd8fa65dc57b3e4afb8204dd 100644 (file)
@@ -7,6 +7,7 @@ __author__ = 'Brian Quinlan (brian@sweetapp.com)'
 
 import atexit
 from concurrent.futures import _base
+import itertools
 import queue
 import threading
 import weakref
@@ -81,6 +82,10 @@ def _worker(executor_reference, work_queue):
         _base.LOGGER.critical('Exception in worker', exc_info=True)
 
 class ThreadPoolExecutor(_base.Executor):
+
+    # Used to assign unique thread names when thread_name_prefix is not supplied.
+    _counter = itertools.count().__next__
+
     def __init__(self, max_workers=None, thread_name_prefix=''):
         """Initializes a new ThreadPoolExecutor instance.
 
@@ -101,7 +106,8 @@ class ThreadPoolExecutor(_base.Executor):
         self._threads = set()
         self._shutdown = False
         self._shutdown_lock = threading.Lock()
-        self._thread_name_prefix = thread_name_prefix
+        self._thread_name_prefix = (thread_name_prefix or
+                                    ("ThreadPoolExecutor-%d" % self._counter()))
 
     def submit(self, fn, *args, **kwargs):
         with self._shutdown_lock:
index 23e95b212447c866a0ec7d282b58cd416d5e60a9..e2da47bc5828de9238652b03493d3837609fc77d 100644 (file)
@@ -172,10 +172,9 @@ class ThreadPoolShutdownTest(ThreadPoolMixin, ExecutorShutdownTest, unittest.Tes
         del executor
 
         for t in threads:
-            # We don't particularly care what the default name is, just that
-            # it has a default name implying that it is a ThreadPoolExecutor
-            # followed by what looks like a thread number.
-            self.assertRegex(t.name, r'^.*ThreadPoolExecutor.*_[0-4]$')
+            # Ensure that our default name is reasonably sane and unique when
+            # no thread_name_prefix was supplied.
+            self.assertRegex(t.name, r'ThreadPoolExecutor-\d+_[0-4]$')
             t.join()
 
 
index d25d191862a8e431c9f9955d7ffe3765cd47d4dd..8e25d62d89377ba454420bb8ca10e0b43d38142c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -371,6 +371,10 @@ Extension Modules
 Library
 -------
 
+- bpo-29212: Fix concurrent.futures.thread.ThreadPoolExecutor threads to have
+  a non repr() based thread name by default when no thread_name_prefix is
+  supplied.  They will now identify themselves as "ThreadPoolExecutor-y_n".
+
 - [Security] bpo-30694: Upgrade expat copy from 2.2.0 to 2.2.1 to get fixes
   of multiple security vulnerabilities including: CVE-2017-9233 (External
   entity infinite loop DoS), CVE-2016-9063 (Integer overflow, re-fix),