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

Class StreamWorker

source code


StreamWorker base class [v1.7+]

The StreamWorker class implements a base (but concrete) Worker that can read and write to multiple streams. Unlike most other Workers, it does not execute any external commands by itself. Rather, it should be pre-bound to "streams", ie. file(s) or file descriptor(s), using the two following methods:

>>> worker.set_reader('stream1', fd1)
>>> worker.set_writer('stream2', fd2)

Like other Workers, the StreamWorker instance should be associated with a Task using task.schedule(worker). When the task engine is ready to process the StreamWorker, all of its streams are being processed together. For that reason, it is not possible to add new readers or writers to a running StreamWorker (ie. task is running and worker is already scheduled).

Configured readers will generate ev_read() events when data is available for reading. So, the following additional public worker variable is available and defines the stream name for the event:

>>> worker.current_sname [ev_read,ev_error]

Please note that ev_error() is called instead of ev_read() when the stream name is 'stderr'. Indeed, all other stream names use ev_read().

Configured writers will allow the use of the method write(), eg. worker.write(data, 'stream2'), to write to the stream.

Instance Methods [hide private]
 
__init__(self, handler, key=None, stderr=False, timeout=-1, autoclose=False, client_class=<class ClusterShell.Worker.Worker.StreamClient at 0x1eba050>)
Initializer.
source code
 
set_reader(self, sname, sfile, retain=True, closefd=True)
Add a readable stream to StreamWorker.
source code
 
set_writer(self, sname, sfile, retain=True, closefd=True)
Set a writable stream to StreamWorker.
source code
 
_engine_clients(self)
Return a list of underlying engine clients.
source code
 
set_key(self, key)
Source key for this worker is free for use.
source code
 
_on_msgline(self, key, msg, sname)
Add a message.
source code
 
_on_timeout(self, key)
Update on timeout.
source code
 
abort(self)
Abort processing any action by this worker.
source code
 
read(self, node=None, sname='stdout')
Read worker stream buffer.
source code
 
write(self, buf, sname=None)
Write to worker.
source code
 
set_write_eof(self, sname=None)
Tell worker to close its writer file descriptor once flushed.
source code

Inherited from Worker: did_timeout, flush_buffers, flush_errors, last_error, last_read

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

Class Variables [hide private]

Inherited from Worker: SNAME_STDERR, SNAME_STDIN, SNAME_STDOUT

Instance Variables [hide private]

Inherited from Worker: current_errmsg, current_msg, current_node, current_rc, current_sname, eh, started, task

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, handler, key=None, stderr=False, timeout=-1, autoclose=False, client_class=<class ClusterShell.Worker.Worker.StreamClient at 0x1eba050>)
(Constructor)

source code 

Initializer. Should be called from derived classes.

Overrides: object.__init__
(inherited documentation)

set_reader(self, sname, sfile, retain=True, closefd=True)

source code 
Add a readable stream to StreamWorker.

Arguments:
    sname   -- the name of the stream (string)
    sfile   -- the stream file or file descriptor
    retain  -- whether the stream retains engine client
               (default is True)
    closefd -- whether to close fd when the stream is closed
               (default is True)

set_writer(self, sname, sfile, retain=True, closefd=True)

source code 
Set a writable stream to StreamWorker.

Arguments:
    sname -- the name of the stream (string)
    sfile -- the stream file or file descriptor
    retain  -- whether the stream retains engine client
               (default is True)
    closefd -- whether to close fd when the stream is closed
               (default is True)

_engine_clients(self)

source code 

Return a list of underlying engine clients.

Overrides: Worker._engine_clients

set_key(self, key)

source code 

Source key for this worker is free for use.

Use this method to set the custom source key for this worker.

abort(self)

source code 

Abort processing any action by this worker.

Overrides: Worker.abort

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')

Overrides: Worker.read

write(self, buf, sname=None)

source code 

Write to worker.

If sname is specified, write to the associated stream, otherwise write to all writable streams.

set_write_eof(self, sname=None)

source code 

Tell worker to close its writer file descriptor once flushed.

Do not perform writes after this call. Like write(), sname can be optionally specified to target a specific writable stream, otherwise all writable streams are marked as EOF.