Skip to content

Commit

Permalink
Resolve multiprocessing deadlock warning by switching to 'spawn' star…
Browse files Browse the repository at this point in the history
…t 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.
  • Loading branch information
EwoutH authored and rht committed Sep 6, 2024
1 parent ee528cb commit 046cd97
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mesa/batchrunner.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import itertools
import multiprocessing
from collections.abc import Iterable, Mapping
from functools import partial
from multiprocessing import Pool
Expand All @@ -8,6 +9,8 @@

from mesa.model import Model

multiprocessing.set_start_method("spawn", force=True)


def batch_run(
model_cls: type[Model],
Expand Down

0 comments on commit 046cd97

Please sign in to comment.