Skip to content

Commit c21c0b9

Browse files
0.29.16
set_start_method()
1 parent da9dfa2 commit c21c0b9

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "spotpython"
10-
version = "0.29.15"
10+
version = "0.29.16"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotpython/spot/spot.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import json
99
from numpy.random import default_rng
1010
from spotpython.design.spacefilling import SpaceFilling
11-
from multiprocessing import set_start_method
11+
from multiprocessing import set_start_method, get_start_method
1212

1313
# old Kriging with attribute "name" kriging
1414
from spotpython.build.kriging import Kriging as OldKriging
@@ -56,9 +56,6 @@
5656
# matplotlib.use("TkAgg")
5757
# matplotlib.use("Agg")
5858

59-
# for multiprocessing:
60-
set_start_method("spawn")
61-
6259
logger = logging.getLogger(__name__)
6360
# configure the handler and formatter as needed
6461
py_handler = logging.FileHandler(f"{__name__}.log", mode="w")
@@ -205,6 +202,13 @@ def __init__(
205202
self.optimizer_control = optimizer_control
206203
self.surrogate_control = surrogate_control
207204

205+
# for multiprocessing:
206+
# check if context is already set to "spawn"
207+
# if not, set it to "spawn"
208+
# Check if the context is already set to "spawn"
209+
if get_start_method(allow_none=True) != "spawn":
210+
set_start_method("spawn", force=True)
211+
208212
# small value:
209213
self.eps = sqrt(spacing(1))
210214

test/test_multiprocess.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from multiprocessing import get_start_method, set_start_method
2+
3+
def test_multiprocessing_context_spawn():
4+
"""
5+
Test that the multiprocessing context is set to 'spawn' if it is not already set.
6+
"""
7+
# Check the current start method
8+
current_method = get_start_method(allow_none=True)
9+
10+
# If the current method is not 'spawn', set it to 'spawn'
11+
if current_method != "spawn":
12+
set_start_method("spawn", force=True)
13+
14+
# Verify that the start method is now 'spawn'
15+
assert get_start_method() == "spawn", "The multiprocessing context should be set to 'spawn'."

0 commit comments

Comments
 (0)