diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index cce8f2833ac5a0..6656eb4940daa1 100644 --- a/Doc/library/unittest.mock.rst +++ b/Doc/library/unittest.mock.rst @@ -2036,6 +2036,22 @@ that proxy attribute access, like the `django settings object `_. +Patching with multiprocessing +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +:func:`patch` modifies objects in the current process's memory. Patches are +not inherited by child processes created with the ``"forkserver"`` or +``"spawn"`` :mod:`multiprocessing` start methods. + +To apply patches in child processes, use a pool initializer:: + + def init_worker(): + mock.patch.object(MyClass, "method", return_value=42).start() + + with multiprocessing.Pool(initializer=init_worker) as pool: + pool.map(my_func, range(5)) + + MagicMock and magic method support ----------------------------------