@@ -97,6 +97,10 @@ To show the individual process IDs involved, here is an expanded example::
97
97
For an explanation of why the ``if __name__ == '__main__' `` part is
98
98
necessary, see :ref: `multiprocessing-programming `.
99
99
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.
100
104
101
105
102
106
.. _multiprocessing-start-methods :
@@ -233,9 +237,12 @@ processes for a different context. In particular, locks created using
233
237
the *fork * context cannot be passed to processes started using the
234
238
*spawn * or *forkserver * start methods.
235
239
236
- A library which wants to use a particular start method should probably
237
- use :func: `get_context ` to avoid interfering with the choice of the
238
- library user.
240
+ Libraries using :mod: `multiprocessing ` or
241
+ :class: `~concurrent.futures.ProcessPoolExecutor ` should be designed to allow
242
+ their users to provide their own multiprocessing context. Using a specific
243
+ context of your own within a library can lead to incompatibilities with the
244
+ rest of the library user's application. Always document if your library
245
+ requires a specific start method.
239
246
240
247
.. warning ::
241
248
@@ -538,9 +545,42 @@ The :mod:`multiprocessing` package mostly replicates the API of the
538
545
to pass to *target *.
539
546
540
547
If a subclass overrides the constructor, it must make sure it invokes the
541
- base class constructor (:meth: ` Process .__init__ `) before doing anything else
548
+ base class constructor (`` super() .__init__() ` `) before doing anything else
542
549
to the process.
543
550
551
+ .. note ::
552
+
553
+ In general, all arguments to :class: `Process ` must be picklable. This is
554
+ frequently observed when trying to create a :class: `Process ` or use a
555
+ :class: `concurrent.futures.ProcessPoolExecutor ` from a REPL with a
556
+ locally defined *target * function.
557
+
558
+ Passing a callable object defined in the current REPL session causes the
559
+ child process to die via an uncaught :exc: `AttributeError ` exception when
560
+ starting as *target * must have been defined within an importable module
561
+ in order to be loaded during unpickling.
562
+
563
+ Example of this uncatchable error from the child::
564
+
565
+ >>> import multiprocessing as mp
566
+ >>> def knigit():
567
+ ... print("Ni!")
568
+ ...
569
+ >>> process = mp.Process(target=knigit)
570
+ >>> process.start()
571
+ >>> Traceback (most recent call last):
572
+ File ".../multiprocessing/spawn.py", line ..., in spawn_main
573
+ File ".../multiprocessing/spawn.py", line ..., in _main
574
+ AttributeError: module '__main__' has no attribute 'knigit'
575
+ >>> process
576
+ <SpawnProcess name='SpawnProcess-1' pid=379473 parent=378707 stopped exitcode=1>
577
+
578
+ See :ref: `multiprocessing-programming-spawn `. While this restriction is
579
+ not true if using the ``"fork" `` start method, as of Python ``3.14 `` that
580
+ is no longer the default on any platform. See
581
+ :ref: `multiprocessing-start-methods `.
582
+ See also :gh: `132898 `.
583
+
544
584
.. versionchanged :: 3.3
545
585
Added the *daemon * parameter.
546
586
@@ -3058,10 +3098,10 @@ start method.
3058
3098
3059
3099
More picklability
3060
3100
3061
- Ensure that all arguments to :meth: ` Process.__init__ ` are picklable.
3062
- Also, if you subclass :class: ` ~multiprocessing. Process` then make sure that
3063
- instances will be picklable when the :meth: ` Process.start
3064
- <multiprocessing.Process.start> ` method is called.
3101
+ Ensure that all arguments to :class: ` ~multiprocessing.Process ` are
3102
+ picklable. Also, if you subclass `` Process.__init__ ``, you must make sure
3103
+ that instances will be picklable when the
3104
+ :meth: ` Process.start <multiprocessing.Process.start> ` method is called.
3065
3105
3066
3106
Global variables
3067
3107
0 commit comments