Skip to content

Commit 4a49a11

Browse files
miss-islingtonaiskwillingcsharktide
authored
[3.13] gh-85222: Document the global start method side effect in multiprocessing (GH-136426) (#142771)
gh-85222: Document the global start method side effect in multiprocessing (GH-136426) * Document the ctx parameter in some types in multiprocessing. * Reduce duplication while still linking to the central explanation from API points with the side effect. (cherry picked from commit 0978b9a) Co-authored-by: AN Long <[email protected]> Co-authored-by: Carol Willing <[email protected]> Co-authored-by: R Chintan Meher <[email protected]>
1 parent f666849 commit 4a49a11

File tree

1 file changed

+72
-14
lines changed

1 file changed

+72
-14
lines changed

Doc/library/multiprocessing.rst

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,21 @@ Reference
507507
The :mod:`multiprocessing` package mostly replicates the API of the
508508
:mod:`threading` module.
509509

510+
.. _global-start-method:
511+
512+
Global start method
513+
^^^^^^^^^^^^^^^^^^^
514+
515+
Python supports several ways to create and initialize a process.
516+
The global start method sets the default mechanism for creating a process.
517+
518+
Several multiprocessing functions and methods that may also instantiate
519+
certain objects will implicitly set the global start method to the system's default,
520+
if it hasn’t been set already. The global start method can only be set once.
521+
If you need to change the start method from the system default, you must
522+
proactively set the global start method before calling functions or methods,
523+
or creating these objects.
524+
510525

511526
:class:`Process` and exceptions
512527
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -877,6 +892,9 @@ For an example of the usage of queues for interprocess communication see
877892
locks/semaphores. When a process first puts an item on the queue a feeder
878893
thread is started which transfers objects from a buffer into the pipe.
879894

895+
Instantiating this class may set the global start method. See
896+
:ref:`global-start-method` for more details.
897+
880898
The usual :exc:`queue.Empty` and :exc:`queue.Full` exceptions from the
881899
standard library's :mod:`queue` module are raised to signal timeouts.
882900

@@ -992,6 +1010,9 @@ For an example of the usage of queues for interprocess communication see
9921010

9931011
It is a simplified :class:`Queue` type, very close to a locked :class:`Pipe`.
9941012

1013+
Instantiating this class may set the global start method. See
1014+
:ref:`global-start-method` for more details.
1015+
9951016
.. method:: close()
9961017

9971018
Close the queue: release internal resources.
@@ -1022,6 +1043,9 @@ For an example of the usage of queues for interprocess communication see
10221043
:class:`JoinableQueue`, a :class:`Queue` subclass, is a queue which
10231044
additionally has :meth:`task_done` and :meth:`join` methods.
10241045

1046+
Instantiating this class may set the global start method. See
1047+
:ref:`global-start-method` for more details.
1048+
10251049
.. method:: task_done()
10261050

10271051
Indicate that a formerly enqueued task is complete. Used by queue
@@ -1134,8 +1158,8 @@ Miscellaneous
11341158
:mod:`multiprocessing` module.
11351159

11361160
If *method* is ``None`` then the default context is returned. Note that if
1137-
the global start method has not been set, this will set it to the
1138-
default method.
1161+
the global start method has not been set, this will set it to the system default
1162+
See :ref:`global-start-method` for more details.
11391163
Otherwise *method* should be ``'fork'``, ``'spawn'``,
11401164
``'forkserver'``. :exc:`ValueError` is raised if the specified
11411165
start method is not available. See :ref:`multiprocessing-start-methods`.
@@ -1146,10 +1170,9 @@ Miscellaneous
11461170

11471171
Return the name of start method used for starting processes.
11481172

1149-
If the global start method has not been set and *allow_none* is
1150-
``False``, then the start method is set to the default and the name
1151-
is returned. If the start method has not been set and *allow_none* is
1152-
``True`` then ``None`` is returned.
1173+
If the global start method is not set and *allow_none* is ``False``, the global start
1174+
method is set to the default, and its name is returned. See
1175+
:ref:`global-start-method` for more details.
11531176

11541177
The return value can be ``'fork'``, ``'spawn'``, ``'forkserver'``
11551178
or ``None``. See :ref:`multiprocessing-start-methods`.
@@ -1376,13 +1399,19 @@ object -- see :ref:`multiprocessing-managers`.
13761399

13771400
A barrier object: a clone of :class:`threading.Barrier`.
13781401

1402+
Instantiating this class may set the global start method. See
1403+
:ref:`global-start-method` for more details.
1404+
13791405
.. versionadded:: 3.3
13801406

13811407
.. class:: BoundedSemaphore([value])
13821408

13831409
A bounded semaphore object: a close analog of
13841410
:class:`threading.BoundedSemaphore`.
13851411

1412+
Instantiating this class may set the global start method. See
1413+
:ref:`global-start-method` for more details.
1414+
13861415
A solitary difference from its close analog exists: its ``acquire`` method's
13871416
first argument is named *block*, as is consistent with :meth:`Lock.acquire`.
13881417

@@ -1397,13 +1426,18 @@ object -- see :ref:`multiprocessing-managers`.
13971426
If *lock* is specified then it should be a :class:`Lock` or :class:`RLock`
13981427
object from :mod:`multiprocessing`.
13991428

1429+
Instantiating this class may set the global start method. See
1430+
:ref:`global-start-method` for more details.
1431+
14001432
.. versionchanged:: 3.3
14011433
The :meth:`~threading.Condition.wait_for` method was added.
14021434

14031435
.. class:: Event()
14041436

14051437
A clone of :class:`threading.Event`.
14061438

1439+
Instantiating this class may set the global start method. See
1440+
:ref:`global-start-method` for more details.
14071441

14081442
.. class:: Lock()
14091443

@@ -1419,6 +1453,9 @@ object -- see :ref:`multiprocessing-managers`.
14191453
instance of ``multiprocessing.synchronize.Lock`` initialized with a
14201454
default context.
14211455

1456+
Instantiating this class may set the global start method. See
1457+
:ref:`global-start-method` for more details.
1458+
14221459
:class:`Lock` supports the :term:`context manager` protocol and thus may be
14231460
used in :keyword:`with` statements.
14241461

@@ -1469,6 +1506,9 @@ object -- see :ref:`multiprocessing-managers`.
14691506
instance of ``multiprocessing.synchronize.RLock`` initialized with a
14701507
default context.
14711508

1509+
Instantiating this class may set the global start method. See
1510+
:ref:`global-start-method` for more details.
1511+
14721512
:class:`RLock` supports the :term:`context manager` protocol and thus may be
14731513
used in :keyword:`with` statements.
14741514

@@ -1521,6 +1561,9 @@ object -- see :ref:`multiprocessing-managers`.
15211561

15221562
A semaphore object: a close analog of :class:`threading.Semaphore`.
15231563

1564+
Instantiating this class may set the global start method. See
1565+
:ref:`global-start-method` for more details.
1566+
15241567
A solitary difference from its close analog exists: its ``acquire`` method's
15251568
first argument is named *block*, as is consistent with :meth:`Lock.acquire`.
15261569

@@ -1649,7 +1692,7 @@ processes.
16491692
attributes which allow one to use it to store and retrieve strings -- see
16501693
documentation for :mod:`ctypes`.
16511694

1652-
.. function:: Array(typecode_or_type, size_or_initializer, *, lock=True)
1695+
.. function:: Array(typecode_or_type, size_or_initializer, *, lock=True, ctx=None)
16531696

16541697
The same as :func:`RawArray` except that depending on the value of *lock* a
16551698
process-safe synchronization wrapper may be returned instead of a raw ctypes
@@ -1663,9 +1706,13 @@ processes.
16631706
automatically protected by a lock, so it will not necessarily be
16641707
"process-safe".
16651708

1666-
Note that *lock* is a keyword-only argument.
1709+
*ctx* is a context object, or ``None`` (use the current context). If ``None``,
1710+
calling this may set the global start method. See
1711+
:ref:`global-start-method` for more details.
16671712

1668-
.. function:: Value(typecode_or_type, *args, lock=True)
1713+
Note that *lock* and *ctx* are keyword-only parameters.
1714+
1715+
.. function:: Value(typecode_or_type, *args, lock=True, ctx=None)
16691716

16701717
The same as :func:`RawValue` except that depending on the value of *lock* a
16711718
process-safe synchronization wrapper may be returned instead of a raw ctypes
@@ -1678,19 +1725,27 @@ processes.
16781725
automatically protected by a lock, so it will not necessarily be
16791726
"process-safe".
16801727

1681-
Note that *lock* is a keyword-only argument.
1728+
*ctx* is a context object, or ``None`` (use the current context). If ``None``,
1729+
calling this may set the global start method. See
1730+
:ref:`global-start-method` for more details.
1731+
1732+
Note that *lock* and *ctx* are keyword-only parameters.
16821733

16831734
.. function:: copy(obj)
16841735

16851736
Return a ctypes object allocated from shared memory which is a copy of the
16861737
ctypes object *obj*.
16871738

1688-
.. function:: synchronized(obj[, lock])
1739+
.. function:: synchronized(obj, lock=None, ctx=None)
16891740

16901741
Return a process-safe wrapper object for a ctypes object which uses *lock* to
16911742
synchronize access. If *lock* is ``None`` (the default) then a
16921743
:class:`multiprocessing.RLock` object is created automatically.
16931744

1745+
*ctx* is a context object, or ``None`` (use the current context). If ``None``,
1746+
calling this may set the global start method. See
1747+
:ref:`global-start-method` for more details.
1748+
16941749
A synchronized wrapper will have two methods in addition to those of the
16951750
object it wraps: :meth:`get_obj` returns the wrapped object and
16961751
:meth:`get_lock` returns the lock object used for synchronization.
@@ -1808,8 +1863,9 @@ their parent process exits. The manager classes are defined in the
18081863
*serializer* must be ``'pickle'`` (use :mod:`pickle` serialization) or
18091864
``'xmlrpclib'`` (use :mod:`xmlrpc.client` serialization).
18101865

1811-
*ctx* is a context object, or ``None`` (use the current context). See the
1812-
:func:`get_context` function.
1866+
*ctx* is a context object, or ``None`` (use the current context). If ``None``,
1867+
calling this may set the global start method. See
1868+
:ref:`global-start-method` for more details.
18131869

18141870
*shutdown_timeout* is a timeout in seconds used to wait until the process
18151871
used by the manager completes in the :meth:`shutdown` method. If the
@@ -2293,7 +2349,9 @@ with the :class:`Pool` class.
22932349
the worker processes. Usually a pool is created using the
22942350
function :func:`multiprocessing.Pool` or the :meth:`Pool` method
22952351
of a context object. In both cases *context* is set
2296-
appropriately.
2352+
appropriately. If ``None``, calling this function will have the side effect
2353+
of setting the current global start method if it has not been set already.
2354+
See the :func:`get_context` function.
22972355

22982356
Note that the methods of the pool object should only be called by
22992357
the process which created the pool.

0 commit comments

Comments
 (0)