cardinal_pythonlib.process¶
Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).
This file is part of cardinal_pythonlib.
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Support functions for process/external command management.
-
cardinal_pythonlib.process.
get_external_command_output
(command: str) → bytes[source]¶ Takes a command-line command, executes it, and returns its
stdout
output.Parameters: command – command string Returns: output from the command as bytes
-
cardinal_pythonlib.process.
get_pipe_series_output
(commands: Sequence[str], stdinput: BinaryIO = None) → bytes[source]¶ Get the output from a piped series of commands.
Parameters: - commands – sequence of command strings
- stdinput – optional
stdin
data to feed into the start of the pipe
Returns: stdout
from the end of the pipe
-
cardinal_pythonlib.process.
kill_proc_tree
(pid: int, including_parent: bool = True, timeout_s: float = 5) → Tuple[Set[psutil.Process], Set[psutil.Process]][source]¶ Kills a tree of processes, starting with the parent. Slightly modified from https://stackoverflow.com/questions/1230669/subprocess-deleting-child-processes-in-windows.
Parameters: - pid – process ID of the parent
- including_parent – kill the parent too?
- timeout_s – timeout to wait for processes to close
Returns: (gone, still_alive)
, where both are sets ofpsutil.Process
objectsReturn type: tuple
-
cardinal_pythonlib.process.
launch_external_file
(filename: str, raise_if_fails: bool = False) → None[source]¶ Launches a file using the operating system’s standard launcher.
Parameters: - filename – file to launch
- raise_if_fails – raise any exceptions from
subprocess.call(["xdg-open", filename])
(Linux) oros.startfile(filename)
(otherwise)? If not, exceptions are suppressed.
-
cardinal_pythonlib.process.
nice_call
(*popenargs, timeout: float = None, cleanup_timeout: float = None, **kwargs) → int[source]¶ Like
subprocess.call()
, but give the child process time to clean up and communicate if aKeyboardInterrupt
is raised.Modified from https://stackoverflow.com/questions/34458583/python-subprocess-call-doesnt-handle-signal-correctly