global_namespace¶
Global namespace management for multiprocessing.
This module provides a thread-safe global namespace that can be transferred across multiprocessing boundaries. It handles the differences between fork and spawn/forkserver start methods by allowing custom parent/child callbacks for state serialization.
Key concepts: - Var: Named variable with optional parent_fn (serialize) and child_fn (deserialize) - fork_init: If True, variable is reinitialized in child (not pickled from parent) - parent_switch(): Called in parent before process creation to capture state - child_switch(): Called in child after process creation to restore state
All data must be pickle-able unless fork_init=True.
Example
import py_misc_utils.global_namespace as gns
v = gns.Var('b4_greeting')
gns.set(v, 'hello')
assert gns.get(v) == 'hello'
# A callable defval is invoked when the variable is unset (force=True).
v2 = gns.Var('b4_level', defval=lambda: 'defaulted')
assert gns.get(v2, force=True) == 'defaulted'
parent_switch(method)
¶
Capture global namespace state for transfer to child process.
Called in the parent process before creating a new process using spawn/forkserver methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Multiprocessing start method ('spawn', 'forkserver', etc.). |
required |
Returns:
| Type | Description |
|---|---|
|
Dictionary mapping variable names to Var instances with serialized data. |
Source code in py_misc_utils/global_namespace.py
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 94 95 96 97 98 99 | |
child_switch(method, ns)
¶
Restore global namespace state in child process.
Called in the child process after creation using spawn/forkserver methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Multiprocessing start method. |
required | |
ns
|
Dictionary of Var instances returned by parent_switch(). |
required |
Source code in py_misc_utils/global_namespace.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
get(var, force=True)
¶
Get a variable's value from the global namespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
var
|
Var instance to retrieve. |
required | |
force
|
If True, create with default value if not present (default: True). |
True
|
Returns:
| Type | Description |
|---|---|
|
The variable's data, or None if not found and force=False. |
Source code in py_misc_utils/global_namespace.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
set(var, data)
¶
Set a variable's value in the global namespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
var
|
Var instance to set. |
required | |
data
|
New data value. |
required |
Returns:
| Type | Description |
|---|---|
|
The previous data value, or None if the variable was not previously |
|
|
set or was set to None. |
Source code in py_misc_utils/global_namespace.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |