You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Final Solution: Environment Variables at Import Time
3
+
## Final Solution
4
4
5
-
Successfully converted multi-process strategy from fork to spawn using standard `multiprocessing` + `dill` + **environment variables**.
5
+
Successfully converted from fork to spawn using:
6
+
1. Standard `multiprocessing` (not `multiprocess` package)
7
+
2.`DillCallable` wrapper for closures
8
+
3. Environment variables for sys.path/cwd (set at import time)
9
+
4. IPCContext passed as argument (not global)
6
10
7
-
## The Critical Insight
11
+
## The Journey
8
12
9
-
**Problem:** When Python spawns a subprocess, it unpickles function arguments BEFORE entering the function. Setting sys.path inside the function is too late.
13
+
### Issue 1: Python 3.14 Compatibility
14
+
- ❌ `multiprocess` package has `subprocess._USE_VFORK` error
15
+
- ✅ Created `DillCallable` wrapper with standard `multiprocessing`
10
16
11
-
**Solution:** Use environment variables that are read at MODULE IMPORT TIME, before any unpickling happens.
17
+
### Issue 2: Module Import Errors
18
+
- ❌ User modules not importable in subprocess (missing sys.path/cwd)
19
+
- ✅ Set via environment variables read at MODULE IMPORT TIME
12
20
13
-
## Implementation
21
+
### Issue 3: Global Variable Not Inherited
22
+
- ❌ `_mp_common.ipc_context` global is `None` in spawn subprocess
23
+
- ✅ Pass `IPCContext` as argument to `subprocess_main`
14
24
15
-
### 1. New Setup Module (_mp_setup.py)
25
+
## Final Implementation
26
+
27
+
### 1. Setup Module (_mp_setup.py) - NEW FILE
28
+
Reads environment at import time (before any unpickling):
16
29
```python
17
30
import json, os, sys
18
31
19
-
# Read environment and configure BEFORE any imports
✅ User modules importable (sys.path set before unpickling)
81
+
✅ No `multiprocess` dependency
82
+
✅ User modules import correctly
83
+
✅ Ready for production
52
84
53
-
## Key Lesson
54
-
With spawn multiprocessing, you cannot set sys.path by passing it as an argument - by the time your function receives it, unpickling has already happened. Use environment variables that are read at module import time instead.
85
+
## Key Insights
86
+
1.**Unpickling happens before function entry** - can't set sys.path by passing as arg
87
+
2.**Environment variables work** - read at module import time, before unpickling
88
+
3.**Globals don't transfer with spawn** - must pass IPCContext as argument
89
+
4.**Import order matters** - _mp_setup must be imported FIRST
0 commit comments