def start_subprocess(self, execution_id: int, command: str, environment: dict, timeout: int) -> None:
"""
Start a subprocess:
- extend the parent process's environment with custom environment variables
- track stdout and stderr file descriptors for later reading
- set process group to facilitate killing any children of the command
:param execution_id: the ID of the Execution instance being run
:param command: a list of arguments, first argument must be an executable
:param environment: environment variables from the WorkflowRun
:param timeout: maximum number of seconds the process should be allowed to run for
"""
process_environment = os.environ.copy()
for key, value in environment.items():
# all variables must be strings, be explicit so it fail in our code
process_environment[key] = str(value)
logger.info('Starting execution #%s', execution_id)
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
preexec_fn=os.setpgrp, env=process_environment, shell=True)
# store references to the process and file descriptors
# Popen gives us io.BufferedReader; get the raw file handle instead
execution = Execution(process, execution_id, timeout)
self.pipes.update({
process.stdout.raw: execution,
process.stderr.raw: execution
})
self.running[execution_id] = execution
评论列表
文章目录