Package ClusterShell :: Package Worker :: Module Worker :: Class Worker
[hide private]
[frames] | no frames]

Class Worker

source code


Worker is an essential base class for the ClusterShell library. The goal of a worker object is to execute a common work on a single or several targets (abstract notion) in parallel. Concret targets and also the notion of local or distant targets are managed by Worker's subclasses (for example, see the DistantWorker base class).

A configured Worker object is associated to a specific ClusterShell Task, which can be seen as a single-threaded Worker supervisor. Indeed, the work to be done is executed in parallel depending on other Workers and Task's current paramaters, like current fanout value.

ClusterShell is designed to write event-driven applications, and the Worker class is key here as Worker objects are passed as parameter of most event handlers (see the ClusterShell.Event.EventHandler class).

The following public object variables are defined on some events, so you may find them useful in event handlers:

Example of use:

>>> from ClusterShell.Event import EventHandler
>>> class MyOutputHandler(EventHandler):
...     def ev_read(self, worker):
...             node = worker.current_node
...             line = worker.current_msg
...             print "%s: %s" % (node, line)
...
Instance Methods [hide private]
 
__init__(self, handler)
Initializer.
source code
 
_set_task(self, task)
Bind worker to task.
source code
 
_task_bound_check(self)
Helper method to check that worker is bound to a task.
source code
 
_engine_clients(self)
Return a list of underlying engine clients.
source code
 
_on_start(self, key)
Called on command start.
source code
 
_on_rc(self, key, rc)
Command return code received.
source code
 
_on_written(self, key, bytes_count, sname)
Notification of bytes written.
source code
 
last_read(self)
Get last read message from event handler.
source code
 
last_error(self)
Get last error message from event handler.
source code
 
did_timeout(self)
Return whether this worker has aborted due to timeout.
source code
 
read(self, node=None, sname='stdout')
Read worker stream buffer.
source code
 
abort(self)
Abort processing any action by this worker.
source code
 
flush_buffers(self)
Flush any messages associated to this worker.
source code
 
flush_errors(self)
Flush any error messages associated to this worker.
source code

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

Class Variables [hide private]
  SNAME_STDIN = 'stdin'
  SNAME_STDOUT = 'stdout'
  SNAME_STDERR = 'stderr'
Instance Variables [hide private]
  eh
associated :class:`.EventHandler`
  task
worker's task when scheduled or None
  started
set to True when worker has started
  current_node
set to node in event handler
  current_msg
set to stdout message in event handler
  current_errmsg
set to stderr message in event handler
  current_rc
set to return code in event handler
  current_sname
set to stream name in event handler
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, handler)
(Constructor)

source code 

Initializer. Should be called from derived classes.

Overrides: object.__init__

_set_task(self, task)

source code 

Bind worker to task. Called by task.schedule().

last_read(self)

source code 

Get last read message from event handler. [DEPRECATED] use current_msg

last_error(self)

source code 

Get last error message from event handler. [DEPRECATED] use current_errmsg

read(self, node=None, sname='stdout')

source code 
Read worker stream buffer.

Return stream read buffer of current worker.

Arguments:
    node -- node name; can also be set to None for simple worker
            having worker.key defined (default is None)
    sname -- stream name (default is 'stdout')