python(5) subprocess and logging
subprocess
You can use the Python subprocess module to create new processes, connect to their input and output, and retrieve their return codes and/or output of the process.
subprocess run
The subprocess.run() method is a convenient way to run a subprocess and wait for it to complete. Once the subprocess is started, the run() method blocks until the subprocess completes and returns a CompletedProcess object, which contains the return code and output of the subprocess.The check argument is an optional argument of the subprocess.run() function in the Python subprocess module. It is a boolean value that controls whether the function should check the return code of the command being run.When check is set to True, the function will check the return code of the command and raise a CalledProcessError exception if the return code is non-zero. The exception will have the return code, stdout, stderr, and command as attributes.
Note that when there is “&” at the end in command, the run will not wait the process to end. When using eval$command in shell, it’s the same. Don’t use &.
subprocess Popen
subprocess.Popen
is a lower-level interface to running subprocesses, while subprocess.run is a higher-level wrapper around Popen that is intended to be more convenient to use. Popen allows you to start a new process and interact with its standard input, output, and error streams. It returns a handle to the running process that can be used to wait for the process to complete, check its return code, or terminate it.
In general, you should use run if you just need to run a command and capture its output and Popen if you need more control over the process, such as interacting with its input and output streams.The Popen class has several methods that allow you to interact with the process, such as communicate(), poll(), wait(), terminate(), and kill().
subprocess call
subprocess.call()
is a function in the Python subprocess module that is used to run a command in a separate process and wait for it to complete. It returns the return code of the command, which is zero if the command was successful, and non-zero if it failed.subprocess.call() is useful when you want to run a command and check the return code, but do not need to capture the output.
subprocess check_output
check_output is a function in the subprocess module that is similar to run(), but it only returns the standard output of the command, and raises a CalledProcessError exception if the return code is non-zero.
Subprocess Pipe
A pipe is a unidirectional communication channel that connects one process’s standard output to another’s standard input. A pipe can connect the output of one command to the input of another, allowing the output of the first command to be used as input to the second command.Pipes can be created using the subprocess module with the Popen class by specifying the stdout or stdin argument as subprocess.PIPE.
logging
Logging provides a set of convenience functions for simple logging usage. These are debug(), info(), warning(), error() and critical().
The default level is WARNING, which means that only events of this level and above will be tracked, unless the logging package is configured to do otherwise.
logging config
logging.basicConfig(format=’%(levelname)s %(asctime)s %(process)d %(message)s’, level=logging.DEBUG)
If not printing after config, note that you should config this before importing other libraries incase the config is overriden.
reference
https://www.datacamp.com/tutorial/python-subprocess
https://docs.python.org/3/howto/logging.html