diff --git a/partcad/src/partcad/runtime_python.py b/partcad/src/partcad/runtime_python.py index 57a1fb5b..fd99816e 100644 --- a/partcad/src/partcad/runtime_python.py +++ b/partcad/src/partcad/runtime_python.py @@ -11,7 +11,7 @@ import pathlib import subprocess import sys - +import threading from . import runtime @@ -23,6 +23,8 @@ def __init__(self, ctx, sandbox, version=None): super().__init__(ctx, "python-" + sandbox + "-" + version) self.version = version + self.lock = threading.Lock() + def run(self, cmd, stdin=""): p = subprocess.Popen( cmd, diff --git a/partcad/src/partcad/runtime_python_conda.py b/partcad/src/partcad/runtime_python_conda.py index 5c6d5d2b..390e279a 100644 --- a/partcad/src/partcad/runtime_python_conda.py +++ b/partcad/src/partcad/runtime_python_conda.py @@ -17,42 +17,44 @@ class CondaPythonRuntime(runtime_python.PythonRuntime): def __init__(self, ctx, version=None): super().__init__(ctx, "conda", version) - if not self.initialized: - self.conda_path = shutil.which("conda") - if self.conda_path is None: - raise Exception( - "ERROR: PartCAD is configured to use conda, but conda is missing" - ) - - try: - os.makedirs(self.path) - subprocess.run( - [ - self.conda_path, - "create", - "-y", - "-p", - self.path, - "python=%s" % version, - ] - ) - subprocess.run(["conda", "install", "-y", "-p", self.path, "pip"]) - self.initialized = True - except Exception as e: - shutil.rmtree(self.path) - raise e + self.conda_path = shutil.which("conda") def run(self, cmd, stdin=""): - return super().run( - [ - self.conda_path, - "run", - "--no-capture-output", - "-p", - self.path, - # "python", # Trust conda to set the link correctly - "python%s" % self.version, # This doesn't work on Windows - ] - + cmd, - stdin, - ) + with self.lock: + if not self.initialized: + if self.conda_path is None: + raise Exception( + "ERROR: PartCAD is configured to use conda, but conda is missing" + ) + + try: + os.makedirs(self.path) + subprocess.run( + [ + self.conda_path, + "create", + "-y", + "-p", + self.path, + "python=%s" % self.version, + ] + ) + subprocess.run(["conda", "install", "-y", "-p", self.path, "pip"]) + self.initialized = True + except Exception as e: + shutil.rmtree(self.path) + raise e + + return super().run( + [ + self.conda_path, + "run", + "--no-capture-output", + "-p", + self.path, + "python", + # "python%s" % self.version, # This doesn't work on Windows + ] + + cmd, + stdin, + )