From 4c71b684a9037305eb9804af54c15d6e27ad0991 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 20 Mar 2024 13:25:25 +0100 Subject: [PATCH] Fix finalizer dependency on global experiment_runner (#346) * bugfix to finalizer fixes a bug in the finalizer in case of working directory models * Update futures_util.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- ema_workbench/em_framework/futures_mpi.py | 2 +- .../em_framework/futures_multiprocessing.py | 2 +- ema_workbench/em_framework/futures_util.py | 25 +++++++++++-------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/ema_workbench/em_framework/futures_mpi.py b/ema_workbench/em_framework/futures_mpi.py index 48b812f50..0f78ea97c 100644 --- a/ema_workbench/em_framework/futures_mpi.py +++ b/ema_workbench/em_framework/futures_mpi.py @@ -65,7 +65,7 @@ def mpi_initializer(models, log_level, root_dir): # setup the working directories tmpdir = setup_working_directories(models, root_dir) if tmpdir: - atexit.register(finalizer, os.path.abspath(tmpdir)) + atexit.register(finalizer(experiment_runner), os.path.abspath(tmpdir)) # _logger.info(f"worker {rank} initialized") root_logger.info(f"worker {rank} initialized") diff --git a/ema_workbench/em_framework/futures_multiprocessing.py b/ema_workbench/em_framework/futures_multiprocessing.py index 43c9174db..68ec28e54 100644 --- a/ema_workbench/em_framework/futures_multiprocessing.py +++ b/ema_workbench/em_framework/futures_multiprocessing.py @@ -66,7 +66,7 @@ def initializer(*args): # remove the root temp if tmpdir: multiprocessing.util.Finalize( - None, finalizer, args=(os.path.abspath(tmpdir),), exitpriority=10 + None, finalizer(experiment_runner), args=(os.path.abspath(tmpdir),), exitpriority=10 ) diff --git a/ema_workbench/em_framework/futures_util.py b/ema_workbench/em_framework/futures_util.py index 055704f5d..061624d37 100644 --- a/ema_workbench/em_framework/futures_util.py +++ b/ema_workbench/em_framework/futures_util.py @@ -30,21 +30,24 @@ def determine_rootdir(msis): return root_dir -def finalizer(tmpdir): +def finalizer(experiment_runner): """cleanup""" - global experiment_runner - _logger.info("finalizing") - experiment_runner.cleanup() - del experiment_runner + def finalizer(tmpdir): + _logger.info("finalizing") - time.sleep(1) + experiment_runner.cleanup() + # del experiment_runner - if tmpdir: - try: - shutil.rmtree(tmpdir) - except OSError: - pass + time.sleep(1) + + if tmpdir: + try: + shutil.rmtree(tmpdir) + except OSError: + pass + + return finalizer def setup_working_directories(models, root_dir):