Skip to content

Commit

Permalink
fix(combine): allow simultaneous calls to combine class from parallel…
Browse files Browse the repository at this point in the history
… python runtimes
  • Loading branch information
engeir committed Nov 21, 2023
1 parent 8127e83 commit 791c721
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions cosmoplots/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from __future__ import annotations

import pathlib
import random
import subprocess
import tempfile


class Combine:
Expand Down Expand Up @@ -162,6 +164,10 @@ def _check_cli_available() -> None:
) from e

def _run_subprocess(self) -> None:
# In case several python runtimes use this class, we prepend a random string so
# that we do not experience conflicts when creating the temporary files.
tmp_dir = tempfile.TemporaryDirectory()
rnd_str = str(random.random())[2:]
if self._w is None or self._h is None:
raise ValueError("You need to specify the files and grid first.")
idx = list(range(len(self._files)))
Expand All @@ -180,7 +186,7 @@ def _run_subprocess(self) -> None:
f"gravity {self._gravity} fill {self._color} text"
f" {self._pos[0]},{self._pos[1]} '{label}'"
),
f"{str(i)}.png",
pathlib.Path(tmp_dir.name) / f"{rnd_str}_{str(i)}.png",
]
)
# Create horizontal subfigures
Expand All @@ -189,23 +195,25 @@ def _run_subprocess(self) -> None:
idx_sub = idx[j * self._w : (j + 1) * self._w]
subprocess.call(
["convert", "+append"]
+ [f"{str(i)}.png" for i in idx_sub]
+ [f"subfigure_{j}.png"]
+ [
pathlib.Path(tmp_dir.name) / f"{rnd_str}_{str(i)}.png"
for i in idx_sub
]
+ [pathlib.Path(tmp_dir.name) / f"{rnd_str}_subfigure_{j}.png"]
)

# Create vertical subfigures from horizontal subfigures
subprocess.call(
["convert", "-append"]
+ [f"subfigure_{j}.png" for j in range(self._h)]
+ [
pathlib.Path(tmp_dir.name) / f"{rnd_str}_subfigure_{j}.png"
for j in range(self._h)
]
+ [self._output.resolve()]
)

# Delete temporary files
subprocess.call(
["rm"]
+ [f"{str(i)}.png" for i in idx]
+ [f"subfigure_{j}.png" for j in range(self._h)]
)
tmp_dir.cleanup()

def help(self) -> None:
"""Print commands that are used."""
Expand Down

0 comments on commit 791c721

Please sign in to comment.