Skip to content

Commit 046cd97

Browse files
EwoutHrht
authored andcommitted
Resolve multiprocessing deadlock warning by switching to 'spawn' start method
This commit addresses a DeprecationWarning related to using `fork()` in multi-threaded processes, which can lead to deadlocks in the child processes when using Python's `multiprocessing` module. - Modified the `batch_run` function to set the multiprocessing start method to 'spawn' by default. - The 'spawn' method avoids the issues that arise from forking in a multi-threaded environment by starting a new Python interpreter process for each worker, which is safer and more compatible with modern systems. - This change ensures that the warning related to multi-threading and `fork()` is resolved across all environments where multiprocessing is used. The change is applied globally to ensure consistent behavior in both code execution and testing environments. The test suite should now run without issuing a DeprecationWarning, and the multiprocessing behavior will be more robust, especially in environments that rely on threads.
1 parent ee528cb commit 046cd97

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

mesa/batchrunner.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import itertools
2+
import multiprocessing
23
from collections.abc import Iterable, Mapping
34
from functools import partial
45
from multiprocessing import Pool
@@ -8,6 +9,8 @@
89

910
from mesa.model import Model
1011

12+
multiprocessing.set_start_method("spawn", force=True)
13+
1114

1215
def batch_run(
1316
model_cls: type[Model],

0 commit comments

Comments
 (0)