Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate execute_subprocess() function #1476

Merged
merged 6 commits into from
Jun 12, 2024
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 51 additions & 39 deletions pyiron_base/jobs/job/runfunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import posixpath
import subprocess
from typing import Optional

from jinja2 import Template
from pyiron_snippets.deprecate import deprecate
Expand Down Expand Up @@ -614,6 +615,46 @@ def wrapper(job):
return wrapper


def execute_subprocess(
executable: str,
shell: bool,
working_directory: str,
conda_environment_name: Optional[str] = None,
conda_environment_path: Optional[str] = None,
) -> str:
if conda_environment_name is None and conda_environment_path is None:
out = subprocess.run(
executable,
cwd=working_directory,
shell=shell,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
check=True,
env=os.environ.copy(),
).stdout
else:
import conda_subprocess

if conda_environment_name is not None:
conda_environment_path = None
else:
conda_environment_name = None
jan-janssen marked this conversation as resolved.
Show resolved Hide resolved

out = conda_subprocess.run(
executable,
cwd=working_directory,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
check=True,
prefix_name=conda_environment_name,
prefix_path=conda_environment_path,
).stdout

return out


@run_time_decorator
def execute_job_with_external_executable(job):
"""
Expand All @@ -634,45 +675,16 @@ def execute_job_with_external_executable(job):
cores=job.server.cores, threads=job.server.threads, gpus=job.server.gpus
)
job_crashed, out = False, None
if (
job.server.conda_environment_name is None
and job.server.conda_environment_path is None
):
try:
out = subprocess.run(
executable,
cwd=job.project_hdf5.working_directory,
shell=shell,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
check=True,
env=os.environ.copy(),
).stdout
except (subprocess.CalledProcessError, FileNotFoundError) as e:
job_crashed, out = handle_failed_job(job=job, error=e)
else:
import conda_subprocess

if job.server.conda_environment_name is not None:
prefix_name = job.server.conda_environment_name
prefix_path = None
else:
prefix_name = None
prefix_path = job.server.conda_environment_path
try:
out = conda_subprocess.run(
executable,
cwd=job.project_hdf5.working_directory,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
check=True,
prefix_name=prefix_name,
prefix_path=prefix_path,
).stdout
except (subprocess.CalledProcessError, FileNotFoundError) as e:
job_crashed, out = handle_failed_job(job=job, error=e)
try:
out = execute_subprocess(
executable=executable,
shell=shell,
working_directory=job.working_directory,
conda_environment_name=job.server.conda_environment_name,
conda_environment_path=job.server.conda_environment_path,
)
except (subprocess.CalledProcessError, FileNotFoundError) as e:
job_crashed, out = handle_failed_job(job=job, error=e)

job._logger.info(
"{}, status: {}, output: {}".format(job.job_info_str, job.status, out)
Expand Down
Loading