Process Status Code Helper Utilities

pstatus

pstatus is can be used to extract meaning from process status codes as returned by os.system, os.wait, os.waitpid, as well as subprocess.call, subprocess.CalledProcessError.returncode, subprocess.Popen.call, subprocess.Popen.wait, and subprocess.Popen.returncode.

It exports a function split which extracts an exit code, a signal number, a flag indicating whether or not the process left a core dump behind.

pstatus.split(status, subprocess=False)

Split an exit status code as returned by os.system, os.wait, or os.waitpid, returning a tuple which includes the processes exit code, the signal responsible for the processes termination, and whether or not the process left a core dump behind. If there was no exit code then the first value will be None, and if there was no signal, then the second value will be None.

Optionally, and perhaps with less utility, if a second flag is passed and set to True, will split a returncode as returned by the subprocess module. In this instance the core part of the returned 3-tuple will always be None, because that information is unavailable.

For example, with os.system:

>>> split(os.system('true'))
Status(exit=0, signal=None, core=False)
>>> split(os.system('false'))
Status(exit=1, signal=None, core=False)

Using os.spawnlp and os.kill to demonstrate extraction of signals:

>>> pid = os.spawnlp(os.P_NOWAIT, 'sleep', 'sleep', '100')
>>> os.kill(pid, 15)
>>> _, code = os.waitpid(pid, 0)
>>> split(code)
Status(exit=None, signal=15, core=False)

And now with subprocess.call:

>>> import subprocess
>>> split(subprocess.call(['true']), subprocess=True)
Status(exit=0, signal=None, core=None)
>>> split(subprocess.call(['false']), subprocess=True)
Status(exit=1, signal=None, core=None)

Using subprocess.Popen and subprocess.Popen.kill to extract signals:

>>> p = subprocess.Popen(['sleep', '100'])
>>> p.terminate()
>>> split(p.wait(), subprocess=True)
Status(exit=None, signal=15, core=None)
Parameters:
  • status (int) – status code to split
  • subprocess (bool) – flag indicating supplied status is a subprocess returncode
Returns:

3-tuple (exit, signal, core)

Return type:

3-tuple

pstatus.os

pstatus.os exports system, wait, wait3, wait4, and waitpid which shadow the functions with the same name in the standard library’s os module, but instead of returning a raw status code, returns a split tuple which includes an exit code, a signal number, a flag indicating whether or not te process has left a core dump.

pstatus.os.system(command)

Execute the command (a string) in a subshell

Split the exit status code into a tuple which includes the processes exit code, the signal responsible for the processes termination, and whether or not the process left a core dump behind. If there was no exit code then the first value will be None, and if there was no signal, then the second value will be None.

>>> system('true')
Status(exit=0, signal=None, core=False)
Parameters:command (string) – command to run
Returns:3-tuple (exit, signal, core)
Return type:3-tuple
pstatus.os.wait()

Wait for completion of a child process.

Returns the pid of the completed child process and it’s exit status code split into a tuple which includes the processes exit code, the signal responsible for the processes termination, and whether or not the process left a core dump behind. If there was no exit code then the first value will be None, and if there was no signal, then the second value will be None.

>>> pid = os.spawnlp(os.P_NOWAIT, 'true', 'true')
>>> wait()  
Status(pid=..., exit=0, signal=None, core=False)
Returns:4-tuple (pid, exit, signal, core)
Return type:4-tuple
pstatus.os.wait3(options)

Wait for completion of a child process.

Returns the pid of the completed child process, it’s rusage, and it’s exit status code split into a tuple which includes the processes exit code, the signal responsible for the processes termination, and whether or not the process left a core dump behind. If there was no exit code then the first value will be None, and if there was no signal, then the second value will be None.

>>> pid = os.spawnlp(os.P_NOWAIT, 'true', 'true')
>>> wait3(0)  
Status(pid=..., exit=0, signal=None, core=False, rusage=...)
Parameters:options (int) – options for wait
Returns:5-tuple (pid, exit, signal, core, rusage)
Return type:5-tuple
pstatus.os.wait4(pid, options)

Wait for completion of a given child process.

Returns the pid of the completed child process, it’s rusage, and it’s exit status code split into a tuple which includes the processes exit code, the signal responsible for the processes termination, and whether or not the process left a core dump behind. If there was no exit code then the first value will be None, and if there was no signal, then the second value will be None.

If pid is greater than 0, waitpid() requests status information for that specific process. If pid is 0, the request is for the status of any child in the process group of the current process. If pid is -1, the request pertains to any child of the current process. If pid is less than -1, status is requested for any process in the process group -pid (the absolute value of pid).

>>> pid = os.spawnlp(os.P_NOWAIT, 'true', 'true')
>>> wait4(pid, 0)  
Status(pid=..., exit=0, signal=None, core=False, rusage=...)
Parameters:
  • pid (int) – pid to wait for
  • options (int) – options for wait
Returns:

5-tuple (pid, exit, signal, core, rusage)

Return type:

5-tuple

pstatus.os.waitpid(pid, options)

Wait for completion of a given child process.

Returns the pid of the completed child process and it’s exit status code split into a tuple which includes the processes exit code, the signal responsible for the processes termination, and whether or not the process left a core dump behind. If there was no exit code then the first value will be None, and if there was no signal, then the second value will be None.

If pid is greater than 0, waitpid() requests status information for that specific process. If pid is 0, the request is for the status of any child in the process group of the current process. If pid is -1, the request pertains to any child of the current process. If pid is less than -1, status is requested for any process in the process group -pid (the absolute value of pid).

>>> pid = os.spawnlp(os.P_NOWAIT, 'true', 'true')
>>> waitpid(pid, 0)  
Status(pid=..., exit=0, signal=None, core=False)
Parameters:
  • pid (int) – pid to wait for
  • options (int) – options for wait
Returns:

4-tuple (pid, exit, signal, core)

Return type:

4-tuple

Indices and tables

Project Versions

Table Of Contents

This Page