Skip to content

multiprocessing

zeus.utils.multiprocessing

Multiprocessing-related utilities.

_is_global_in_spawned_child

_is_global_in_spawned_child()

Return True if called from module-level code in a spawned child's main script.

In a spawned child process (using the "spawn" start method), the main script is re-imported with __name__ = "__mp_main__" instead of "__main__". This function detects if we're currently executing module-level code (like global variable initialization) in such a script.

This walks the call stack looking for any frame from a real Python file (not multiprocessing infrastructure like or ) where the module's __name__ is "__mp_main__".

Source code in zeus/utils/multiprocessing.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def _is_global_in_spawned_child() -> bool:
    """Return True if called from module-level code in a spawned child's main script.

    In a spawned child process (using the "spawn" start method), the main script
    is re-imported with `__name__ = "__mp_main__"` instead of `"__main__"`. This
    function detects if we're currently executing module-level code (like global
    variable initialization) in such a script.

    This walks the call stack looking for any <module> frame from a real Python
    file (not multiprocessing infrastructure like <string> or <frozen ...>) where
    the module's `__name__` is `"__mp_main__"`.
    """
    frame = inspect.currentframe()
    if frame is None:
        return False
    frame = frame.f_back
    while frame is not None:
        if frame.f_code.co_name == "<module>":
            filename = frame.f_code.co_filename
            # Skip multiprocessing infrastructure frames
            if not (filename == "<string>" or filename.startswith("<frozen")):
                module_name = frame.f_globals.get("__name__", "")
                if module_name == "__mp_main__":
                    return True
        frame = frame.f_back
    return False

warn_if_global_in_subprocess

warn_if_global_in_subprocess(self)

Warn when a monitor is created at import time in a spawned subprocess.

This detects a common pitfall where ZeusMonitor (or related classes) is instantiated as a global variable or called from module-level code. When the script spawns subprocesses using the "spawn" method, the subprocess re-imports the main module, causing global initialization code to run again (e.g., loading DNN models), leading to OOM errors.

Parameters:

Name Type Description Default
self Any

The instance being constructed (used to derive class name).

required
Source code in zeus/utils/multiprocessing.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def warn_if_global_in_subprocess(self: Any) -> None:
    """Warn when a monitor is created at import time in a spawned subprocess.

    This detects a common pitfall where ZeusMonitor (or related classes) is
    instantiated as a global variable or called from module-level code.
    When the script spawns subprocesses using the "spawn" method, the
    subprocess re-imports the main module, causing global initialization
    code to run again (e.g., loading DNN models), leading to OOM errors.

    Args:
        self: The instance being constructed (used to derive class name).
    """
    if not _is_global_in_spawned_child():
        return
    class_name = type(self).__name__
    warnings.warn(
        f"{class_name} was instantiated during module import in a spawned subprocess. "
        "This usually means the monitor was created as a global variable, so the child "
        "process re-imported your main module and executed global code again. Move monitor "
        'construction under `if __name__ == "__main__":` or inside a function to avoid '
        "repeated imports and memory issues.",
        stacklevel=4,
    )

get_or_raise_if_dead

get_or_raise_if_dead(result_q, process, poll_interval=1.0)

Wait for one item from result_q, raising if process exits without sending it.

result_q.get() alone blocks forever if the helper process dies first, for example from an uncaught exception.

Parameters:

Name Type Description Default
result_q Queue

Queue the helper process puts its result on.

required
process BaseProcess

The helper process.

required
poll_interval float

Seconds between checks that the helper process is still alive.

1.0

Raises:

Type Description
RuntimeError

If process exits without putting a result on result_q.

Source code in zeus/utils/multiprocessing.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def get_or_raise_if_dead(result_q: Queue, process: BaseProcess, poll_interval: float = 1.0) -> Any:
    """Wait for one item from `result_q`, raising if `process` exits without sending it.

    `result_q.get()` alone blocks forever if the helper process dies first, for example from
    an uncaught exception.

    Args:
        result_q: Queue the helper process puts its result on.
        process: The helper process.
        poll_interval: Seconds between checks that the helper process is still alive.

    Raises:
        RuntimeError: If `process` exits without putting a result on `result_q`.
    """
    while True:
        try:
            return result_q.get(timeout=poll_interval)
        except queue.Empty:
            if process.is_alive():
                continue
            # The process may have sent its result just before exiting.
            try:
                return result_q.get(timeout=poll_interval)
            except queue.Empty:
                raise RuntimeError(
                    f"The background process {process.name} exited with code {process.exitcode} "
                    "before returning a result. Its traceback, if any, is printed above."
                ) from None