Skip to content

Commit 415f28b

Browse files
committed
bpo-29212: Fix the ugly repr() TPE 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.
1 parent dcc8ce4 commit 415f28b

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-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
@@ -81,6 +82,10 @@ def _worker(executor_reference, work_queue):
8182
_base.LOGGER.critical('Exception in worker', exc_info=True)
8283

8384
class ThreadPoolExecutor(_base.Executor):
85+
86+
# Used to assign unique thread names when thread_name_prefix is not supplied.
87+
_counter = itertools.count().__next__
88+
8489
def __init__(self, max_workers=None, thread_name_prefix=''):
8590
"""Initializes a new ThreadPoolExecutor instance.
8691
@@ -101,7 +106,8 @@ def __init__(self, max_workers=None, thread_name_prefix=''):
101106
self._threads = set()
102107
self._shutdown = False
103108
self._shutdown_lock = threading.Lock()
104-
self._thread_name_prefix = thread_name_prefix
109+
self._thread_name_prefix = (thread_name_prefix or
110+
("ThreadPoolExecutor-%d" % self._counter()))
105111

106112
def submit(self, fn, *args, **kwargs):
107113
with self._shutdown_lock:

Lib/test/test_concurrent_futures.py

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

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

181180

0 commit comments

Comments
 (0)