qb :: backend :: pythonChildHandler :: PyCmdDispatcher :: Class PyCmdDispatcher
[hide private]
[frames] | no frames]

Class PyCmdDispatcher

source code


The PyCmdDispatcher class is used by qube python jobtypes to execute commands through an abstracted application or shell.

The jobtype can have several sets of commands, defined as either a list of strings or a single string, for job setup, job teardown, and per-agendaItem commands.

Command Execution

Commands are sent as string to the child process via stdin. The child is responsible for communicating the success/failure of the command over a FIFO-like object known as the backChannel. A successful command execution results in a zero being written to the backChannel by the child. A non-zero indicates some sort of an error occurred attempting to execute the command.

What Constitutes Command Failure

In the case of a python child process, failure is defined as any command that raises an uncaught exception. The traceback is passed as a string from the child to the parent to be printed to sys.stderr.

Instance Methods [hide private]
 
__init__(self, logHandler=None, mergeStderr=False, debug=False)
init function to set up members of the class
source code
 
__startupBackChannel(self)
Instantiate a PFXSimpleSocketServer and start it in another thread
source code
int
__send(self, msg)
Send a string to the child process's stdin.
source code
tuple
__getBackChannelBuffer(self)
Read the first 4 bytes from the PFXSimpleServer's buffer; the first 4 bytes which are expected to contain the payload length as an integer in a packed format.
source code
 
startChild(self, subprocessArgs)
Start up a child process, which will actually do the work for the job.
source code
int
execute(self, commands, work=None)
Pass the commands in a list to a child process one by one for execution by eval.
source code
int
close(self, exitcode=0)
Signal the child process to shut down.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables [hide private]
int CHILD_CHECK_INTERVAL = 0.2
how long (in seconds) to sleep between checks to see if the child process is finsihed executing the command
int LOG_PARSE_INTERVAL = 5
how long (in seconds) to wait between parsing the logs for errors and other regex matches
int BLOCKSIZE
Blocksize for reading from an i/o stream.
int POLL_TIMEOUT
Timeout value in milliseconds for select.poll() objects.
Instance Variables [hide private]
PFXSimpleSocketServer backChannel
The "out of band" pipe used to communicate success/failure from the PyCmdExecutor to the PyCmdDispatcher; distinct from the PyCmdExecutor's stdout/stderr, avoids having to parse the child's stdout/stderr for a sign that the last-sent command has completed executing.
subprocess.Popen child
The running child process, which will contain a PyCmdExecutor instance.
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, logHandler=None, mergeStderr=False, debug=False)
(Constructor)

source code 

init function to set up members of the class

Parameters:
  • logHandler (function) - a code object, it's the logHandler() method of the backend class that's running the job that's instantiating this.
  • mergeStderr (bool) - whether to merge stderr->stdout
Overrides: object.__init__

__send(self, msg)

source code 

Send a string to the child process's stdin.

Parameters:
  • msg (string) - string to send
Returns: int
the message length, or -1 if the child is non-responsive

__getBackChannelBuffer(self)

source code 

Read the first 4 bytes from the PFXSimpleServer's buffer; the first 4 bytes which are expected to contain the payload length as an integer in a packed format. Retrieve the message

Returns: tuple
The message length and the message, message length is -1 if the pipe has closed.

startChild(self, subprocessArgs)

source code 

Start up a child process, which will actually do the work for the job.

Parameters:
  • subprocessArgs (list) - arg is a 3-element array, suitable for passing as the first parameter to subprocess.Popen.
    1. full path the the shell eg:'/bin/tcsh'
    2. '-c'
    3. the entire command to run to launch the child process, as a single string

execute(self, commands, work=None)

source code 

Pass the commands in a list to a child process one by one for execution by eval.

Parameters:
  • commands (list

    The following two forms yield identical results:

       >>> cmds = ['import sys', 'print sys.version_info' ]
       >>> execute(cmds)
    
       >>> execute('import sys')
       >>> execute('print sys.version_info')
    
    ) - A list (optionally a string for a single command) of commands to be executed by a child process. If commands is a string, it will be re-cast as a single-element list.
Returns: int
0 for success, 1 for failure.

close(self, exitcode=0)

source code 

Signal the child process to shut down.

Parameters:
  • exitcode (int) - Suggested exit code of the child process.
Returns: int
Actual value the child exited with.