Skip to content

Commit

Permalink
Parallelize generate testmodels (#1309)
Browse files Browse the repository at this point in the history
Fixes #1302

On my machine, that reduces the runtime of `pixi run
generate-testmodels` from 1m50s to 42s
  • Loading branch information
Hofer-Julian authored Mar 25, 2024
1 parent f43010b commit 0acbbed
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions utils/generate-testmodels.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import multiprocessing
import shutil
from functools import partial
from pathlib import Path

import ribasim_testmodels


def generate_model(args, datadir):
model_name, model_constructor = args
model = model_constructor()
model.write(datadir / model_name / "ribasim.toml")
return model_name


if __name__ == "__main__":
datadir = Path("generated_testmodels")
if datadir.is_dir():
Expand All @@ -18,7 +28,10 @@
Don't put important stuff in here, it will be emptied for every run."""
)

for model_name, model_constructor in ribasim_testmodels.constructors.items():
print(f"Generating {model_name}")
model = model_constructor()
model.write(datadir / model_name / "ribasim.toml")
generate_model_partial = partial(generate_model, datadir=datadir)

with multiprocessing.Pool(processes=4) as p:
for model_name in p.imap_unordered(
generate_model_partial, ribasim_testmodels.constructors.items()
):
print(f"Generated {model_name}")

0 comments on commit 0acbbed

Please sign in to comment.