diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst index 3c87bbe8e59809..9a217bc3ca711b 100644 --- a/Doc/library/multiprocessing.rst +++ b/Doc/library/multiprocessing.rst @@ -2775,7 +2775,7 @@ returns an instance of :class:`ThreadPool`, which is a subclass of worker threads rather than worker processes. -.. class:: ThreadPool([processes[, initializer[, initargs]]]) +.. class:: ThreadPool([processes[, initializer[, initargs[, name]]]]) A thread pool object which controls a pool of worker threads to which jobs can be submitted. :class:`ThreadPool` instances are fully interface diff --git a/Lib/multiprocessing/pool.py b/Lib/multiprocessing/pool.py index f979890170b1a1..fa0d0fb458e7c0 100644 --- a/Lib/multiprocessing/pool.py +++ b/Lib/multiprocessing/pool.py @@ -181,7 +181,7 @@ def Process(ctx, *args, **kwds): return ctx.Process(*args, **kwds) def __init__(self, processes=None, initializer=None, initargs=(), - maxtasksperchild=None, context=None): + maxtasksperchild=None, context=None, name='Process-Pool'): # Attributes initialized early to make sure that they exist in # __del__() if __init__() raises an exception self._pool = [] @@ -198,6 +198,7 @@ def __init__(self, processes=None, initializer=None, initargs=(), self._maxtasksperchild = maxtasksperchild self._initializer = initializer self._initargs = initargs + self._name = name if processes is None: processes = os.process_cpu_count() or 1 @@ -224,6 +225,7 @@ def __init__(self, processes=None, initializer=None, initargs=(), sentinels = self._get_sentinels() self._worker_handler = threading.Thread( + name='{}-Worker-Handler'.format(self._name), target=Pool._handle_workers, args=(self._cache, self._taskqueue, self._ctx, self.Process, self._processes, self._pool, self._inqueue, self._outqueue, @@ -236,6 +238,7 @@ def __init__(self, processes=None, initializer=None, initargs=(), self._task_handler = threading.Thread( + name='{}-Task-Handler'.format(self._name), target=Pool._handle_tasks, args=(self._taskqueue, self._quick_put, self._outqueue, self._pool, self._cache) @@ -245,6 +248,7 @@ def __init__(self, processes=None, initializer=None, initargs=(), self._task_handler.start() self._result_handler = threading.Thread( + name='{}-Result-Handler'.format(self._name), target=Pool._handle_results, args=(self._outqueue, self._quick_get, self._cache) ) @@ -309,17 +313,20 @@ def _repopulate_pool(self): self._outqueue, self._initializer, self._initargs, self._maxtasksperchild, - self._wrap_exception) + self._wrap_exception, + self._name) @staticmethod def _repopulate_pool_static(ctx, Process, processes, pool, inqueue, outqueue, initializer, initargs, - maxtasksperchild, wrap_exception): + maxtasksperchild, wrap_exception, + name): """Bring the number of pool processes up to the specified number, for use after reaping workers which have exited. """ for i in range(processes - len(pool)): w = Process(ctx, target=worker, + name='{}-Worker-{}'.format(name, i), args=(inqueue, outqueue, initializer, initargs, maxtasksperchild, @@ -926,8 +933,8 @@ def Process(ctx, *args, **kwds): from .dummy import Process return Process(*args, **kwds) - def __init__(self, processes=None, initializer=None, initargs=()): - Pool.__init__(self, processes, initializer, initargs) + def __init__(self, processes=None, initializer=None, initargs=(), name='Thread-Pool'): + Pool.__init__(self, processes, initializer, initargs, name='Thread-Pool') def _setup_queues(self): self._inqueue = queue.SimpleQueue() diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-10-13-06-46-00.gh-issue-79177.Y4AAxY.rst b/Misc/NEWS.d/next/Core and Builtins/2023-10-13-06-46-00.gh-issue-79177.Y4AAxY.rst new file mode 100644 index 00000000000000..2632750a1dd77d --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-10-13-06-46-00.gh-issue-79177.Y4AAxY.rst @@ -0,0 +1 @@ +Add name parameter to the multiprocessing module. Patch by Joannah Nanjekye.