Skip to content

Commit 7d8282d

Browse files
authored
[3.6] bpo-29212: Fix the ugly repr() ThreadPoolExecutor thread name. (GH-2315) (#3276)
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.. (cherry picked from commit a3d91b4)
1 parent 31b8efe commit 7d8282d

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

Lib/concurrent/futures/thread.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import atexit
99
from concurrent.futures import _base
10+
import itertools
1011
import queue
1112
import threading
1213
import weakref
@@ -83,6 +84,10 @@ def _worker(executor_reference, work_queue):
8384
_base.LOGGER.critical('Exception in worker', exc_info=True)
8485

8586
class ThreadPoolExecutor(_base.Executor):
87+
88+
# Used to assign unique thread names when thread_name_prefix is not supplied.
89+
_counter = itertools.count().__next__
90+
8691
def __init__(self, max_workers=None, thread_name_prefix=''):
8792
"""Initializes a new ThreadPoolExecutor instance.
8893
@@ -103,7 +108,8 @@ def __init__(self, max_workers=None, thread_name_prefix=''):
103108
self._threads = set()
104109
self._shutdown = False
105110
self._shutdown_lock = threading.Lock()
106-
self._thread_name_prefix = thread_name_prefix
111+
self._thread_name_prefix = (thread_name_prefix or
112+
("ThreadPoolExecutor-%d" % self._counter()))
107113

108114
def submit(self, fn, *args, **kwargs):
109115
with self._shutdown_lock:

Lib/test/test_concurrent_futures.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,9 @@ def test_thread_names_default(self):
191191
del executor
192192

193193
for t in threads:
194-
# We don't particularly care what the default name is, just that
195-
# it has a default name implying that it is a ThreadPoolExecutor
196-
# followed by what looks like a thread number.
197-
self.assertRegex(t.name, r'^.*ThreadPoolExecutor.*_[0-4]$')
194+
# Ensure that our default name is reasonably sane and unique when
195+
# no thread_name_prefix was supplied.
196+
self.assertRegex(t.name, r'ThreadPoolExecutor-\d+_[0-4]$')
198197
t.join()
199198

200199

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix concurrent.futures.thread.ThreadPoolExecutor threads to have a non repr()
2+
based thread name by default when no thread_name_prefix is supplied. They will
3+
now identify themselves as "ThreadPoolExecutor-y_n".

0 commit comments

Comments
 (0)