Skip to content

[3.6] bpo-29212: Fix the ugly repr() ThreadPoolExecutor thread name #3276

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

Merged
merged 1 commit into from
Sep 3, 2017
Merged
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
8 changes: 7 additions & 1 deletion Lib/concurrent/futures/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import atexit
from concurrent.futures import _base
import itertools
import queue
import threading
import weakref
Expand Down Expand Up @@ -83,6 +84,10 @@ def _worker(executor_reference, work_queue):
_base.LOGGER.critical('Exception in worker', exc_info=True)

class ThreadPoolExecutor(_base.Executor):

# Used to assign unique thread names when thread_name_prefix is not supplied.
_counter = itertools.count().__next__

def __init__(self, max_workers=None, thread_name_prefix=''):
"""Initializes a new ThreadPoolExecutor instance.

Expand All @@ -103,7 +108,8 @@ def __init__(self, max_workers=None, thread_name_prefix=''):
self._threads = set()
self._shutdown = False
self._shutdown_lock = threading.Lock()
self._thread_name_prefix = thread_name_prefix
self._thread_name_prefix = (thread_name_prefix or
("ThreadPoolExecutor-%d" % self._counter()))

def submit(self, fn, *args, **kwargs):
with self._shutdown_lock:
Expand Down
7 changes: 3 additions & 4 deletions Lib/test/test_concurrent_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,9 @@ def test_thread_names_default(self):
del executor

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


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix concurrent.futures.thread.ThreadPoolExecutor threads to have a non repr()
based thread name by default when no thread_name_prefix is supplied. They will
now identify themselves as "ThreadPoolExecutor-y_n".