Skip to content

Commit c909643

Browse files
committed
Backport ThreadPoolExecutor thread name from upstream
Changes from python/cpython#2315
1 parent 4cbaaff commit c909643

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

concurrent/futures/thread.py

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

66
import atexit
77
from concurrent.futures import _base
8+
import itertools
89
import Queue as queue
910
import threading
1011
import weakref
@@ -90,6 +91,10 @@ def _worker(executor_reference, work_queue):
9091

9192

9293
class ThreadPoolExecutor(_base.Executor):
94+
95+
# Used to assign unique thread names when thread_name_prefix is not supplied.
96+
_counter = itertools.count().next
97+
9398
def __init__(self, max_workers=None, thread_name_prefix=''):
9499
"""Initializes a new ThreadPoolExecutor instance.
95100
@@ -110,7 +115,8 @@ def __init__(self, max_workers=None, thread_name_prefix=''):
110115
self._threads = set()
111116
self._shutdown = False
112117
self._shutdown_lock = threading.Lock()
113-
self._thread_name_prefix = thread_name_prefix
118+
self._thread_name_prefix = (thread_name_prefix or
119+
("ThreadPoolExecutor-%d" % self._counter()))
114120

115121
def submit(self, fn, *args, **kwargs):
116122
with self._shutdown_lock:

test_futures.py

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

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

257256

0 commit comments

Comments
 (0)