Package shelljob :: Module proc :: Class Group
[hide private]
[frames] | no frames]

Class Group

source code

Runs a subprocess in parallel, capturing it's output and providing non-blocking reads (well, at least for the caller they appear non-blocking).

Instance Methods [hide private]
 
__init__(self) source code
 
run(self, cmd, shell=False)
Adds a new process to this object.
source code
 
_run_impl(self, cmd, shell) source code
 
readlines(self, max_lines=1000, timeout=2.0)
Reads available lines from any of the running processes.
source code
 
readline(self, timeout=2.0)
Read a single line from any running process.
source code
 
is_pending(self)
Determine if calling readlines would actually yield any output.
source code
 
count_running(self)
Return the number of processes still running.
source code
 
get_exit_codes(self)
Return a list of all processes and their exit code.
source code
 
clear_finished(self)
Remove all finished processes from the managed list.
source code
Method Details [hide private]

run(self, cmd, shell=False)

source code 

Adds a new process to this object. This process is run and the output collected.

Parameters:
  • cmd - the command to execute. This may be an array as passed to Popen, or a string, which will be parsed by 'shlex.split'
Returns:
the handle to the process return from Popen

readlines(self, max_lines=1000, timeout=2.0)

source code 

Reads available lines from any of the running processes. If no lines are available now it will wait until 'timeout' to read a line. If nothing is running the timeout is not waited and the function simply returns.

When a process has been completed and all output has been read from it, a variable 'group_ouput_done' will be set to True on the process handle.

Parameters:
  • timeout - how long to wait if there is nothing available now
  • max_lines - maximum number of lines to get at once
Returns:
An array of tuples of the form: ( handle, line ) There 'handle' was returned by 'run' and 'line' is the line which is read. If no line is available an empty list is returned.

readline(self, timeout=2.0)

source code 

Read a single line from any running process.

Note that this will end up blocking for timeout once all processes have completed. 'readlines' however can properly handle that situation and stop reading once everything is complete.

Returns:
Tuple of ( handle, line ) or None if no output generated.

is_pending(self)

source code 

Determine if calling readlines would actually yield any output. This returns true if there is a process running or there is data in the queue.

count_running(self)

source code 

Return the number of processes still running. Note that although a process may be finished there could still be output from it in the queue. You should use 'is_pending' to determine if you should still be reading.

get_exit_codes(self)

source code 

Return a list of all processes and their exit code.

Returns:
A list of tuples: ( handle, exit_code ) 'handle' as returned from 'run' 'exit_code' of the process or None if it has not yet finished