@@ -97,6 +97,10 @@ To show the individual process IDs involved, here is an expanded example::
9797For an explanation of why the ``if __name__ == '__main__' `` part is
9898necessary, see :ref: `multiprocessing-programming `.
9999
100+ The arguments to :class: `Process ` usually need to be unpickleable from within
101+ the child process. If you tried typing the above example directly into a REPL it
102+ could lead to an :exc: `AttributeError ` in the child process trying to locate the
103+ *f * function in the ``__main__ `` module.
100104
101105
102106.. _multiprocessing-start-methods :
@@ -107,6 +111,8 @@ Contexts and start methods
107111Depending on the platform, :mod: `multiprocessing ` supports three ways
108112to start a process. These *start methods * are
109113
114+ .. _multiprocessing-start-method-spawn :
115+
110116 *spawn *
111117 The parent process starts a fresh Python interpreter process. The
112118 child process will only inherit those resources necessary to run
@@ -117,6 +123,8 @@ to start a process. These *start methods* are
117123
118124 Available on POSIX and Windows platforms. The default on Windows and macOS.
119125
126+ .. _multiprocessing-start-method-fork :
127+
120128 *fork *
121129 The parent process uses :func: `os.fork ` to fork the Python
122130 interpreter. The child process, when it begins, is effectively
@@ -137,6 +145,8 @@ to start a process. These *start methods* are
137145 raise a :exc: `DeprecationWarning `. Use a different start method.
138146 See the :func: `os.fork ` documentation for further explanation.
139147
148+ .. _multiprocessing-start-method-forkserver :
149+
140150 *forkserver *
141151 When the program starts and selects the *forkserver * start method,
142152 a server process is spawned. From then on, whenever a new process
@@ -218,9 +228,12 @@ processes for a different context. In particular, locks created using
218228the *fork * context cannot be passed to processes started using the
219229*spawn * or *forkserver * start methods.
220230
221- A library which wants to use a particular start method should probably
222- use :func: `get_context ` to avoid interfering with the choice of the
223- library user.
231+ Libraries using :mod: `multiprocessing ` or
232+ :class: `~concurrent.futures.ProcessPoolExecutor ` should be designed to allow
233+ their users to provide their own multiprocessing context. Using a specific
234+ context of your own within a library can lead to incompatibilities with the
235+ rest of the library user's application. Always document if your library
236+ requires a specific start method.
224237
225238.. warning ::
226239
@@ -518,9 +531,42 @@ The :mod:`multiprocessing` package mostly replicates the API of the
518531 to pass to *target *.
519532
520533 If a subclass overrides the constructor, it must make sure it invokes the
521- base class constructor (:meth: ` Process .__init__ `) before doing anything else
534+ base class constructor (`` super() .__init__() ` `) before doing anything else
522535 to the process.
523536
537+ .. note ::
538+
539+ In general, all arguments to :class: `Process ` must be picklable. This is
540+ frequently observed when trying to create a :class: `Process ` or use a
541+ :class: `concurrent.futures.ProcessPoolExecutor ` from a REPL with a
542+ locally defined *target * function.
543+
544+ Passing a callable object defined in the current REPL session causes the
545+ child process to die via an uncaught :exc: `AttributeError ` exception when
546+ starting as *target * must have been defined within an importable module
547+ in order to be loaded during unpickling.
548+
549+ Example of this uncatchable error from the child::
550+
551+ >>> import multiprocessing as mp
552+ >>> def knigit():
553+ ... print("Ni!")
554+ ...
555+ >>> process = mp.Process(target=knigit)
556+ >>> process.start()
557+ >>> Traceback (most recent call last):
558+ File ".../multiprocessing/spawn.py", line ..., in spawn_main
559+ File ".../multiprocessing/spawn.py", line ..., in _main
560+ AttributeError: module '__main__' has no attribute 'knigit'
561+ >>> process
562+ <SpawnProcess name='SpawnProcess-1' pid=379473 parent=378707 stopped exitcode=1>
563+
564+ See :ref: `multiprocessing-programming-spawn `. While this restriction is
565+ not true if using the ``"fork" `` start method, as of Python ``3.14 `` that
566+ is no longer the default on any platform. See
567+ :ref: `multiprocessing-start-methods `.
568+ See also :gh: `132898 `.
569+
524570 .. versionchanged :: 3.3
525571 Added the *daemon * parameter.
526572
@@ -2985,6 +3031,9 @@ Beware of replacing :data:`sys.stdin` with a "file like object"
29853031
29863032 For more information, see :issue: `5155 `, :issue: `5313 ` and :issue: `5331 `
29873033
3034+ .. _multiprocessing-programming-spawn :
3035+ .. _multiprocessing-programming-forkserver :
3036+
29883037The *spawn * and *forkserver * start methods
29893038^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
29903039
@@ -2993,10 +3042,10 @@ start method.
29933042
29943043More picklability
29953044
2996- Ensure that all arguments to :meth: ` Process.__init__ ` are picklable.
2997- Also, if you subclass :class: ` ~multiprocessing. Process` then make sure that
2998- instances will be picklable when the :meth: ` Process.start
2999- <multiprocessing.Process.start> ` method is called.
3045+ Ensure that all arguments to :class: ` ~multiprocessing.Process ` are
3046+ picklable. Also, if you subclass `` Process.__init__ ``, you must make sure
3047+ that instances will be picklable when the
3048+ :meth: ` Process.start <multiprocessing.Process.start> ` method is called.
30003049
30013050Global variables
30023051
0 commit comments