Skip to content

bpo-45339: Allow user to specify Thread class for use with ThreadPoolExecutor #28640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions Lib/concurrent/futures/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class ThreadPoolExecutor(_base.Executor):
_counter = itertools.count().__next__

def __init__(self, max_workers=None, thread_name_prefix='',
initializer=None, initargs=()):
initializer=None, initargs=(), thread_class=threading.Thread):
"""Initializes a new ThreadPoolExecutor instance.

Args:
Expand Down Expand Up @@ -157,6 +157,7 @@ def __init__(self, max_workers=None, thread_name_prefix='',
("ThreadPoolExecutor-%d" % self._counter()))
self._initializer = initializer
self._initargs = initargs
self._thread_class = thread_class

def submit(self, fn, /, *args, **kwargs):
with self._shutdown_lock, _global_shutdown_lock:
Expand Down Expand Up @@ -191,11 +192,11 @@ def weakref_cb(_, q=self._work_queue):
if num_threads < self._max_workers:
thread_name = '%s_%d' % (self._thread_name_prefix or self,
num_threads)
t = threading.Thread(name=thread_name, target=_worker,
args=(weakref.ref(self, weakref_cb),
self._work_queue,
self._initializer,
self._initargs))
t = self._thread_class(name=thread_name, target=_worker,
args=(weakref.ref(self, weakref_cb),
self._work_queue,
self._initializer,
self._initargs))
t.start()
self._threads.add(t)
_threads_queues[t] = self._work_queue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Currently the only way to use a custom thread class with the ThreadPoolExecutor is extend the class and override the private method _adjust_thread_count(), which means reproducing (basically, copying and pasting) the body of that method into application code. This change would allow the thread class to be specified when the ThreadPoolExecutor is instantiated, allowing for more composable use.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NEWS entry should describe the change, not argue for it.

Suggested change
Currently the only way to use a custom thread class with the ThreadPoolExecutor is extend the class and override the private method _adjust_thread_count(), which means reproducing (basically, copying and pasting) the body of that method into application code. This change would allow the thread class to be specified when the ThreadPoolExecutor is instantiated, allowing for more composable use.
Add ``thread_class`` parameter to `:class:concurrent.futures.ThreadPoolExecutor`.