Skip to content

Commit 246be21

Browse files
authored
gh-136470: Correct InterpreterPoolExecutor's default thread name (GH-136472)
The OS thread name is now correctly prefixed with `InterpreterPoolExecutor` instead of `ThreadPoolExecutor`.
1 parent aec7f5f commit 246be21

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

Lib/concurrent/futures/interpreter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,7 @@ def __init__(self, max_workers=None, thread_name_prefix='',
118118
each worker interpreter.
119119
initargs: A tuple of arguments to pass to the initializer.
120120
"""
121+
thread_name_prefix = (thread_name_prefix or
122+
(f"InterpreterPoolExecutor-{self._counter()}"))
121123
super().__init__(max_workers, thread_name_prefix,
122124
initializer, initargs)

Lib/test/test_concurrent_futures/test_interpreter_pool.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _thread
12
import asyncio
23
import contextlib
34
import io
@@ -498,6 +499,20 @@ def test_import_interpreter_pool_executor(self):
498499
self.assertEqual(p.stdout.decode(), '')
499500
self.assertEqual(p.stderr.decode(), '')
500501

502+
def test_thread_name_prefix(self):
503+
self.assertStartsWith(self.executor._thread_name_prefix,
504+
"InterpreterPoolExecutor-")
505+
506+
@unittest.skipUnless(hasattr(_thread, '_get_name'), "missing _thread._get_name")
507+
def test_thread_name_prefix_with_thread_get_name(self):
508+
def get_thread_name():
509+
import _thread
510+
return _thread._get_name()
511+
512+
# Some platforms (Linux) are using 16 bytes to store the thread name,
513+
# so only compare the first 15 bytes (without the trailing \n).
514+
self.assertStartsWith(self.executor.submit(get_thread_name).result(),
515+
"InterpreterPoolExecutor-"[:15])
501516

502517
class AsyncioTest(InterpretersMixin, testasyncio_utils.TestCase):
503518

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Correct :class:`concurrent.futures.InterpreterPoolExecutor`'s default thread
2+
name.

0 commit comments

Comments
 (0)