Package asycamore :: Module dispatcher :: Class IDispatchContext

Class IDispatchContext



object --+
         |
        IDispatchContext

The bridge between a service model and the IO dispatch loop.

Implementations of this interface must be compatible with the dispatcher parameter of the iterate api

Implementors are not required to derive from this class but MUST provide a conforming implementation of each method. set_dispatch_filter, socket_closed and set_poll_dispatch_filter are typically called from server or connection implementations while dispatch_once is called by the main control loop of a program or thread.

See asycamore.tcpsocket for the basic primitives necessary for implementing tcp based dispatch targets



Instance Methods
 
dispatch_once(self, timeout, error_in_handler)
Perform the implementation specific dispatch.
 
set_dispatch_filter(self, o, R=True, W=True, E=True)
Tells the dispatch loop what kind of events o is interested in.
 
set_poll_dispatch_filter(self, o, flags)
Set implementation specific event flags or value for o
 
socket_closed(self, fd, o=None)
Called when a socket dispatch target is closed.

Inherited from object: __delattr__, __getattribute__, __hash__, __init__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Properties

Inherited from object: __class__

Method Details

dispatch_once(self, timeout, error_in_handler)

 

Perform the implementation specific dispatch.

Typically this involves a single call to select or poll followed by a call to the dispatch_event method of each dispatch target for which events are pending. Implementations are required to convert any event details into the (R,W,E) form, respectively Readable, Writeable and Exceptional. Provided that each field evaluates False when there are no events for that field and True otherwise; implementations should as far as possible preserve the implementation specific values. Eg., for poll, the R, W, E fields are obtained as:

R, W, E = (
flags & (select.POLLIN | select.POLLPRI),
flags & select.POLLOUT,
flags & select.POLLERR | select.POLLHUP | select.POLLNVAL
)

See asycamore.servicemodel for further discussion of the event format and how dispatch_once can co-operate with service model implementations.

If error_in_handler is not None and an exception other than KeyboardInterrupt or SystemExit is raised when dispatch target is called it is called like:

error_in_handler(o, exc_info)

If error_in_handler not provided or the provided implementation raises a further exception then the exception propogates to the caller.

NOTE: KeyboardInterupt or SystemExit NEVER get passed to error_in_handler

Parameters:
  • timeout - None or 0 are always legal and behave as per sleep(0). Otherwise, an Implementation specific value determining the maximum you would ideally allow the OS to wait before deciding there are no events of interest. All implementations are required to support 0 and None as per sleep(0).
  • error_in_handler - The optional error handler for exceptions raised in a dispatch target

set_dispatch_filter(self, o, R=True, W=True, E=True)

 

Tells the dispatch loop what kind of events o is interested in.

To completely remove o from the dispatch pass any value that evaluates false for each of R, W and E.

Parameters:
  • o - A dispatch target.
  • R - Pass None or False to disable read events for o. Pass True to enable them.
  • W - Pass None or False to disable write events for o. Pass True to enable them
  • E - Pass None or False to disable exceptional events for o. Pass True to enable them.

set_poll_dispatch_filter(self, o, flags)

 

Set implementation specific event flags or value for o

The implementation of this interface is specific to the underlying dispatch mechanism (select, poll, etc). Callers should only make use of it if set_dispatch_filter is unable to express what you want.

Implementations are free to change the api signature provided the first parameter is always the target o. An implementation must be able to express set_dispatch_filter as a subset of the features offered by set_poll_dispatch_filter and, where those features intersect, ensure they behave identically. For example, in the POLL case, flags=0 must be equivalent to set_dispatch_filter(o, R=None, W=None, E=None)

Parameters:
  • o - The dispatch target. This instance must have an attribute fd that stores the dispatch implementation specific key for the target instance o.

socket_closed(self, fd, o=None)

 

Called when a socket dispatch target is closed.

Dispatch implementations should use this to perform any necessary clean up. All o instances are required to arrange that this api be called when their underlying socket connection is closed.

Parameters:
  • fd - The socket .fileno() that was used to key o
  • o - May be None, otherwise it must be the instance that was associated with fd