Skip to content

Commit 2e49b52

Browse files
bpo-38501: Add a warning section to multiprocessing.Pool docs about resource managing (GH-19466)
(cherry picked from commit 7ec43a7) Co-authored-by: Pablo Galindo <[email protected]>
1 parent 9e5f159 commit 2e49b52

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

Doc/library/multiprocessing.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ process which created it.
429429
>>> def f(x):
430430
... return x*x
431431
...
432-
>>> p.map(f, [1,2,3])
432+
>>> with p:
433+
... p.map(f, [1,2,3])
433434
Process PoolWorker-1:
434435
Process PoolWorker-2:
435436
Process PoolWorker-3:
@@ -2100,6 +2101,16 @@ with the :class:`Pool` class.
21002101
Note that the methods of the pool object should only be called by
21012102
the process which created the pool.
21022103

2104+
.. warning::
2105+
:class:`multiprocessing.pool` objects have internal resources that need to be
2106+
properly managed (like any other resource) by using the pool as a context manager
2107+
or by calling :meth:`close` and :meth:`terminate` manually. Failure to do this
2108+
can lead to the process hanging on finalization.
2109+
2110+
Note that is **not correct** to rely on the garbage colletor to destroy the pool
2111+
as CPython does not assure that the finalizer of the pool will be called
2112+
(see :meth:`object.__del__` for more information).
2113+
21032114
.. versionadded:: 3.2
21042115
*maxtasksperchild*
21052116

0 commit comments

Comments
 (0)