Package asycamore :: Module dispatcher

Module dispatcher



The dispatch api: service model & client program mainloop integration

This module provides a means for service models and client programs to co-operate with the main dispatch loop. A default dispatch loop is provided by iterate. The api is defined by IDispatchTarget, IDispatchContext and the the semantics of the before_dispatch and after_dispatch callbacks as ilustrated in the referece iterate function.

iterate may be used directly or as a reference for integrating with foreign main loops.

The dispatch api and the facilities of asycamore.tcpsocket can be used to implement useful client and server programs independently of the servicemodel api (asycamore.servicemodel)

Examples include:

See asycamore.tcpsocket for the basic primitives necessary for implementing the dispatch targets that form the bridge between the dispatch api and the service model api.

before_dispatch and after_dispatch

iterate and any compatible implementation is required to support these callbacks. They provide the necessary hooks for service models and clients to implement there runtime behaviour. Either callback may be None. The loop control values, along with any others provided by the caller of iterate are passed to the each callback as the dispatchvars parameter. The values include the callbacks them selves and, additionally, count, timeout, and error_in_handler. iterate will honour any changes made to these values. For example, changing or disabling the callbacks. Other than meeting its obligations to keep them updated iterate ignores the values timeout and error_in_handler and simply forwards them as the corresponding arguments to IDispatchContext.dispatch_once

Note that either callback may cause loop termination after N further dispatches by doing dispatchvars['count'] = N. And, immediate termination by simply raising StopIteration.



Classes
  IDispatchContext
The bridge between a service model and the IO dispatch loop.
  IDispatchTarget
The minimal state for any asycamore.dispatch.IDispatchContext target
Functions
 
iterate(dispatcher, timeout=30.0, count=-1, dispatchvars=None, before_dispatch=None, after_dispatch=None, error_in_handler=None)
A suitable main loop for service models and clients
Function Details

iterate(dispatcher, timeout=30.0, count=-1, dispatchvars=None, before_dispatch=None, after_dispatch=None, error_in_handler=None)

 

A suitable main loop for service models and clients

Iteratively calls the IDispatchContext.dispatch_once method provided by the dispatcher parameter, interleaving each call between calls to before_dispatch and after_dispatch. The parameters timeout and error_in_handler are passed on to dispatch_once.

If count is initially > -1 then the loop terminates after count calls to dispatcher.dispatch_once. The check occurs immediately after before_dispatch has been called and prior to calling dispatch_once. Note that passing count==0 will cause the loop to terminate after before_dispatch but without having called dispatcher.dispatch_once.

Parameters:
  • dispatcher - Implementor of the IDispatchContext interface.
  • timeout - Passed to dispatcher.dispatch_once as the timeout for the underlying OS call used by the dispatch context implementation.
  • count - The initial value of the loop counter. -1 means loop forever. An integer value > -1 limits the number of calls that will be made to dispatcher.dispatch_once. Note that if either before_dispatch or after_dispatch are provided then they may update this value while the loop is running.
  • dispatchvars - None or provides additional context values for before_dispatch and after_dispatch. Pass an empty dictionary if you want to return state from the before/after callbacks.
  • before_dispatch - None or a callable that is invoked before each call to dispatcher.dispatch_once. If this function raises StopIteration the loop terminates immediately.
  • after_dispatch - None or a callable that is invoked after each call to dispatcher.dispatch_once. If this function raises StopIteration the loop terminates immediately.