Kaydet (Commit) 7d8282d2 authored tarafından Gregory P. Smith's avatar Gregory P. Smith Kaydeden (comit) GitHub

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

bpo-29212: Fix the ugly ThreadPoolExecutor 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..
(cherry picked from commit a3d91b43)
üst 31b8efea
...@@ -7,6 +7,7 @@ __author__ = 'Brian Quinlan (brian@sweetapp.com)' ...@@ -7,6 +7,7 @@ __author__ = 'Brian Quinlan (brian@sweetapp.com)'
import atexit import atexit
from concurrent.futures import _base from concurrent.futures import _base
import itertools
import queue import queue
import threading import threading
import weakref import weakref
...@@ -83,6 +84,10 @@ def _worker(executor_reference, work_queue): ...@@ -83,6 +84,10 @@ def _worker(executor_reference, work_queue):
_base.LOGGER.critical('Exception in worker', exc_info=True) _base.LOGGER.critical('Exception in worker', exc_info=True)
class ThreadPoolExecutor(_base.Executor): 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=''): def __init__(self, max_workers=None, thread_name_prefix=''):
"""Initializes a new ThreadPoolExecutor instance. """Initializes a new ThreadPoolExecutor instance.
...@@ -103,7 +108,8 @@ class ThreadPoolExecutor(_base.Executor): ...@@ -103,7 +108,8 @@ class ThreadPoolExecutor(_base.Executor):
self._threads = set() self._threads = set()
self._shutdown = False self._shutdown = False
self._shutdown_lock = threading.Lock() 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): def submit(self, fn, *args, **kwargs):
with self._shutdown_lock: with self._shutdown_lock:
......
...@@ -191,10 +191,9 @@ class ThreadPoolShutdownTest(ThreadPoolMixin, ExecutorShutdownTest, BaseTestCase ...@@ -191,10 +191,9 @@ class ThreadPoolShutdownTest(ThreadPoolMixin, ExecutorShutdownTest, BaseTestCase
del executor del executor
for t in threads: for t in threads:
# We don't particularly care what the default name is, just that # Ensure that our default name is reasonably sane and unique when
# it has a default name implying that it is a ThreadPoolExecutor # no thread_name_prefix was supplied.
# followed by what looks like a thread number. self.assertRegex(t.name, r'ThreadPoolExecutor-\d+_[0-4]$')
self.assertRegex(t.name, r'^.*ThreadPoolExecutor.*_[0-4]$')
t.join() t.join()
......
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".
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment