tfep.utils.cli.launcher.Launcher
- class tfep.utils.cli.launcher.Launcher[source]
Bases:
objectRuns an executable as a standard command line subprocess.
The class can run a command that is specified either as a list of strings (in the same format used by the standard module
subprocess) or as atfep.utils.cli.CLITool. See the documentation ofCLIToolfor details on how to create a CLI wrapper compatible withLauncher.Note
When commands are run in a different working directory, relative file/directory paths passed as command arguments are interpreted relative to the specified working directory.
To avoid this behavior, you can either specify absolute paths or use the CLI options
tfep.utils.cli.AbsolutePathOptionwhen wrapping your command withCLITool.See also
tfep.utils.cli.CLIToolCLI wrapper utility.
Examples
>>> launcher = Launcher() >>> result = launcher.run(['echo', 'print this'], capture_output=True, text=True) >>> print(result.stdout.strip()) print this
For more complicated cases, it may be convenient to use the CLI wrapping utilities in tfep.utils.cli
>>> import tfep.utils.cli >>> class Echo(tfep.utils.cli.CLITool): ... EXECUTABLE_PATH = 'echo' ... >>> echo_cmd = Echo('print this') >>> result = launcher.run(echo_cmd, capture_output=True, text=True) >>> print(result.stdout.strip()) print this
It is possible to launch multiple commands in parallel and obtain the results of all of them.
>>> results = launcher.run(Echo('print1'), Echo('print2'), capture_output=True, text=True) >>> for res in results: ... print(res.stdout.strip()) ... print1 print2
- __init__()
Methods
__init__()run(*commands[, capture_output, timeout, ...])Run one or more subprocesses in parallel.
- run(*commands, capture_output=False, timeout=None, check=False, stdin=None, stdout=None, stderr=None, cwd=None, **kwargs)[source]
Run one or more subprocesses in parallel.
The method runs all the commands in parallel and waits for all of them to complete, collects their output (if
capture_outputis set) and return them.Currently, the method supports all keyword arguments supported by the
subprocess.Popen. Moreover, it handles running multiple processes in parallel, and, for some of thesubprocess.Popensuch asstdoutandcwd, allow to specify process-specific arguments.- Parameters:
commands (List[str] or tfep.utils.cli.CLITool) – One or more commands to execute, either in the same list format used by
subprocess.Popenor as aCLITool.capture_output (bool, optional) – If
True, stdout and stderr will be captured and returned as an attribute of thesubprocess.CompletedProcessobjects. IfTrue, this overwrites the values of thestdoutandstderrarguments.timeout (float, optional) – The timeout (in seconds) is passed to
Popen.communicate. If it expires for any of the processes,subprocess.TimeoutExpirederror is raised.check (bool, optional) – If
Trueand the exit code of any of the subprocesses was non-zero, asubprocess.CalledProcessErrorerror is raised.stdin (optional) – This can take any value accepted by
subprocess.Popen. If multiple commands are run, this can be a list specifying one stdin per process.stdout (optional) – This can take any value accepted by
subprocess.Popen. If multiple commands are run, this can be a list specifying one stdout per process.stderr (optional) – This can take any value accepted by
subprocess.Popen. If multiple commands are run, this can be a list specifying one stderr per process.cwd (str or List[str], optional) – This can take any value accepted by
subprocess.Popen. If multiple commands are run, this can be a list specifying one current working directory per process.**kwargs – Other keyword arguments to pass to
subprocess.Popen.
- Returns:
result – The object encapsulating the results of the project. If multiple processes are run in parallel, this is a
listof results, one for each process.- Return type:
subprocess.CompletedProcess or List[subprocess.CompletedProcess]
- Raises:
subprocess.CalledProcessError – If any of the run processes returned a non-zero status and
checkisTrue.subprocess.TimeoutExpired`` – If
timeoutwas set and the timeout expired.
See also
subprocess.runStandard library function to run commands.
subprocess.PopenBasic interface to run subprocesses.