From bcb97ee17786939c1146fa87f97dd523b34361e9 Mon Sep 17 00:00:00 2001 From: hrimov Date: Thu, 2 Jul 2026 14:42:33 +0200 Subject: [PATCH] Document mock.patch with multiprocessing start methods --- Doc/library/unittest.mock.rst | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index cce8f2833ac5a02..6656eb4940daa14 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 ----------------------------------