Skip to content

Commit 731eb5d

Browse files
committed
Force to use spawn for multiprocessing
1 parent 8eb9c8b commit 731eb5d

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

PyMaSC/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
import logging
1313
import sys
1414
import traceback
15+
import multiprocessing
1516
from functools import wraps
1617
from typing import Any, Callable
1718

1819
VERSION = "0.3.1"
1920
WEBSITE_URL = "https://pymasc.sb.ecei.tohoku.ac.jp/"
2021

22+
logger = logging.getLogger(__name__)
23+
2124

2225
def logging_version(logger: Any) -> None:
2326
"""Log PyMaSC and Python version information.
@@ -34,6 +37,22 @@ def logging_version(logger: Any) -> None:
3437
logger.debug(line)
3538

3639

40+
def _ensure_spawn() -> None:
41+
"""Ensure the multiprocessing start method is set to `spawn`."""
42+
try:
43+
current = multiprocessing.get_start_method(allow_none=True)
44+
except TypeError:
45+
current = None
46+
if current != "spawn":
47+
try:
48+
multiprocessing.set_start_method("spawn")
49+
except RuntimeError:
50+
logger.warning(
51+
"Failed to set multiprocessing start method to 'spawn'. "
52+
"This may cause issues with multiprocessing in PyMaSC."
53+
)
54+
55+
3756
def entrypoint(logger: Any) -> Callable[[Callable[[], None]], Callable[[], None]]:
3857
"""Decorator for main entry point exception handling.
3958
@@ -57,6 +76,7 @@ def _entrypoint_wrapper_base(main_func: Callable[[], None]) -> Callable[[], None
5776
@wraps(main_func)
5877
def _inner() -> None:
5978
try:
79+
_ensure_spawn()
6080
main_func()
6181
logger.info("PyMASC finished.")
6282
except KeyboardInterrupt:

0 commit comments

Comments
 (0)