Skip to content

Commit

Permalink
Use "spawn" start method for multiprocessing
Browse files Browse the repository at this point in the history
This avoids issues caused by mixing forking and threading in the same
process. The default for multiprocessing will be changed to "spawn"
in python 3.14, but this prevents warnings from being emitted until
then.
  • Loading branch information
jfrost-mo committed Jan 27, 2025
1 parent 023fba1 commit 015517a
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/CSET/operators/ageofair.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,10 @@ def compute_ageofair(
logging.info("STARTING AOA DIAG...")
start = datetime.datetime.now()

# Use "spawn" method to avoid warnings before the default is changed in
# python 3.14. See the (not very good) warning here:
# https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods
mp_context = multiprocessing.get_context("spawn")
# Main call for calculating age of air diagnostic
if ensemble_mode:
for e in range(0, len(XWIND.coord("realization").points)):
Expand All @@ -425,7 +429,7 @@ def compute_ageofair(
tmpdir.name,
)
if multicore:
with multiprocessing.Pool(num_usable_cores) as pool:
with mp_context.Pool(num_usable_cores) as pool:
pool.map(func, range(0, XWIND.shape[4]))
else:
# Convert to list to ensure everything is processed.
Expand All @@ -452,7 +456,7 @@ def compute_ageofair(
tmpdir.name,
)
if multicore:
with multiprocessing.Pool(num_usable_cores) as pool:
with mp_context.Pool(num_usable_cores) as pool:
pool.map(func, range(0, XWIND.shape[3]))
else:
# Convert to list to ensure everything is processed.
Expand Down

0 comments on commit 015517a

Please sign in to comment.