10000 bpo-17140: Document multiprocessing's ThreadPool (GH-23812) (GH-23836) · python/cpython@00278d4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 00278d4

Browse files
bpo-17140: Document multiprocessing's ThreadPool (GH-23812) (GH-23836)
Up until now, the `multiprocessing.pool.ThreadPool` class has gone undocumented, despite being a public class in multiprocessing that is included in `multiprocessing.pool.__all__`. (cherry picked from commit 84ebcf2) Co-authored-by: Matt Wozniski <mwozniski@bloomberg.net>
1 parent 47f075d commit 00278d4

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

Doc/library/multiprocessing.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,6 +2617,46 @@ The :mod:`multiprocessing.dummy` module
26172617
:mod:`multiprocessing.dummy` replicates the API of :mod:`multiprocessing` but is
26182618
no more than a wrapper around the :mod:`threading` module.
26192619

2620+
.. currentmodule:: multiprocessing.pool
2621+
2622+
In particular, the ``Pool`` function provided by :mod:`multiprocessing.dummy`
2623+
returns an instance of :class:`ThreadPool`, which is a subclass of
2624+
:class:`Pool` that supports all the same method calls but uses a pool of
2625+
worker threads rather than worker processes.
2626+
2627+
2628+
.. class:: ThreadPool([processes[, initializer[, initargs]]])
2629+
2630+
A thread pool object which controls a pool of worker threads to which jobs
2631+
can be submitted. :class:`ThreadPool` instances are fully interface
2632+
compatible with :class:`Pool` instances, and their resources must also be
2633+
properly managed, either by using the pool as a context manager or by
2634+
calling :meth:`~multiprocessing.pool.Pool.close` and
2635+
:meth:`~multiprocessing.pool.Pool.terminate` manually.
2636+
2637+
*processes* is the number of worker threads to use. If *processes* is
2638+
``None`` then the number returned by :func:`os.cpu_count` is used.
2639+
2640+
If *initializer* is not ``None`` then each worker process will call
2641+
``initializer(*initargs)`` when it starts.
2642+
2643+
Unlike :class:`Pool`, *maxtasksperchild* and *context* cannot be provided.
2644+
2645+
.. note::
2646+
2647+
A :class:`ThreadPool` shares the same interface as :class:`Pool`, which
2648+
is designed around a pool of processes and predates the introduction of
2649+
the :class:`concurrent.futures` module. As such, it inherits some
2650+
operations that don't make sense for a pool backed by threads, and it
2651+
has its own type for representing the status of asynchronous jobs,
2652+
:class:`AsyncResult`, that is not understood by any other libraries.
2653+
2654+
Users should generally prefer to use
2655+
:class:`concurrent.futures.ThreadPoolExecutor`, which has a simpler
2656+
interface that was designed around threads from the start, and which
2657+
returns :class:`concurrent.futures.Future` instances that are
2658+
compatible with many other libraries, including :mod:`asyncio`.
2659+
26202660

26212661
.. _multiprocessing-programming:
26222662

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add documentation for the :class:`multiprocessing.pool.ThreadPool` class.

0 commit comments

Comments
 (0)
0