From 8f571545844ea2b405a71d854e7413170f8775b6 Mon Sep 17 00:00:00 2001 From: Mohsen Rahmati Yami Date: Wed, 9 Sep 2026 23:40:08 +0330 Subject: [PATCH] gh-157216: Document that forkserver preload skips deferred imports --- Doc/library/multiprocessing.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst index bedea46cb16d60d..1099e8206cda575 100644 --- a/Doc/library/multiprocessing.rst +++ b/Doc/library/multiprocessing.rst @@ -1242,6 +1242,16 @@ Miscellaneous processes. This can be used as a performance enhancement to avoid repeated work in every process. + Only the modules named in *module_names* are imported, together with + whatever those modules import at module level. A module imported inside a + function body is not imported in the forkserver process, and is therefore + imported again in every child process, the first time that function runs + there. For example, preloading :mod:`datetime` does not cover + ``_strptime``, which :meth:`~datetime.datetime.strptime` imports on its + first call, so every child pays for that import privately. To share such + modules as well, call the function at the module level of a preloaded + module, so that the import happens in the forkserver process. + For this to work, it must be called before the forkserver process has been launched (before creating a :class:`Pool` or starting a :class:`Process`).