tea.process Module

tea.process.execute(command, *args, **kwargs)

Execute a command with arguments and wait for output. Arguments should not be quoted!

Keyword arguments:

Parameters:
  • env (dict) – Dictionary of additional environment variables.
  • wait (bool) – Wait for the process to finish.

Example:

>>> code = 'import sys;sys.stdout.write('out');sys.exit(0)'
>>> status, out, err = execute('python', '-c', code)
>>> print('status: %s, output: %s, error: %s' % (status, out, err))
status: 0, output: out, error:
>>> code = 'import sys;sys.stderr.write('out');sys.exit(1)'
>>> status, out, err = execute('python', '-c', code)
>>> print('status: %s, output: %s, error: %s' % (status, out, err))
status: 1, output: , error: err
tea.process.execute_and_report(command, *args, **kwargs)

Executes a command with arguments and wait for output. If execution was successful function will return True, if not, it will log the output using standard logging and return False.

tea.process.get_processes(sort_by_name=True, cmdline=False)

Retrieves a list of processes sorted by name.

Parameters:
  • sort_by_name (bool) – Sort the list by name or by process ID’s
  • cmdline (bool) – Add process command line to output
Return type:

list[(int, str)] or list[(int, str, str)]

Returns:

List of process id, process name and optional cmdline tuples

tea.process.find(name, arg=None)

Find process by name or by argument in command line if arg param is available.

Parameters:
  • name (str) – process name to search for
  • arg (str) – command line argument for a process to search for
Return type:

(int, str)

Returns:

A tuple of process id, process name

tea.process.kill(pid)

Kills a process by it’s process ID.

Parameters:pid (int) – Process ID of the process to kill.
class tea.process.base.Process(command, arguments=None, env=None, redirect_output=True, working_dir=None)

Abstract base class for the Process class that is implemented for every platform in it’s own module.

Simple example of Process class usage can be:

>>> from tea.process import Process
>>> p = Process('python', ['-c', 'import time;time.sleep(5);print 3'])
>>> p.start()
>>> p.is_running
True
>>> p.wait()
True
>>> p.read()
'3\n'
>>> p.eread()
''

Creates the Process object providing the command and it’s command line arguments.

The only required parameter is the command to execute. It’s important to note that the constructor only initializes the class, it doesn’t executes the process. To actually execute the process you have to call :met:`start`.

Parameters:
  • command (str) – Path to the executable file.
  • arguments (list) – list of command line arguments passed to the command
  • env (dict) – Optional additional environment variables that will be added to the subprocess environment or that override currently set environment variables.
  • redirect_output (bool) – True if you want to be able to get the standard output and the standard error of the subprocess, otherwise it will be redirected to /dev/null
  • working_dir (str) – Set the working directory from which the process will be started.
start()

Starts the process.

kill()

Kills the process if it’s running.

wait(timeout=None)

Waits for the process to finish.

It will wait for the process to finish running. If the timeout is provided, the function will wait only timeout amount of seconds and then return to it’s caller.

Parameters:timeout (None or int) – None if you want to wait to wait until the process actually finishes, otherwise it will wait just the timeout number of seconds.
Return type:bool
Returns:Return value only makes sense if you provided the timeout parameter. It will indicate if the process actually finished in the amount of time specified, i.e. if the we specify 3 seconds and the process actually stopped after 3 seconds it will return True otherwise it will return False.
is_running

Property that indicates if the process is still running.

Return type:bool
Returns:True if the process is still running False otherwise
pid

Property that returns the PID of the process if it is running.

Return type:int
Returns:process id of the running process
exit_code

Property that returns the exit code if the process has finished running.

Return type:int or None
Returns:Exit code or None if the process is still running
write(string)

Write a string to the process standard input.

Parameters:string (str) – String to write to the process standard input
read()

Read from the process standard output.

Return type:str
Returns:The data process has written to the standard output if it has written anything. If it hasn’t or you already read all the data process wrote, it will return an empty string.
eread()

Read from the process standard error.

Return type:str
Returns:The data process has written to the standard error if it has written anything. If it hasn’t or you already read all the data process wrote, it will return an empty string.