From 0c1e4facb8761f9993f10fa858ca7abdc13143e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Sun, 24 Mar 2024 18:11:09 +0100 Subject: [PATCH] BUG: avoid multiprocessing overhead when looping on a single worker --- nonos/main.py | 20 ++++++++++++-------- tests/test_plotting.py | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/nonos/main.py b/nonos/main.py index 1f1d9443..03c852ce 100644 --- a/nonos/main.py +++ b/nonos/main.py @@ -46,7 +46,7 @@ import importlib_resources -# process function for parallisation purpose with progress bar +# process function for parallelisation purpose with progress bar # counterParallel = Value('i', 0) # initialization of a counter def process_field( on, @@ -540,14 +540,18 @@ def mytrack(iterable, *args, **kwargs): # noqa: ARG001 logger.info("Starting main loop") tstart = time.time() - with Pool(ncpu) as pool: - list( - mytrack( - pool.imap(func, args["on"]), - description="Processing snapshots", - total=len(args["on"]), + if ncpu == 1: + for on in args["on"]: + func(on) + else: + with Pool(ncpu) as pool: + list( + mytrack( + pool.imap(func, args["on"]), + description="Processing snapshots", + total=len(args["on"]), + ) ) - ) if not show: logger.info("Operation took {:.2f}s", time.time() - tstart) diff --git a/tests/test_plotting.py b/tests/test_plotting.py index e33852d3..1409cd71 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -190,7 +190,7 @@ def test_pbar(simulation_dir, capsys, tmp_path): out, err = capsys.readouterr() assert err == "" - assert "Processing snapshots" in out + assert out == "" assert ret == 0