parley/helpers.py - helper classes and functions for constructing actors.
class Dispatcher:
Inherit from this class to create an actor that behaves as an RPC server: messages received get translated into method calls, and method return values (as well as exceptions) are translated into reply messages. If you override the __init__ method, call the parent __init__ last to start the actor.def _default(self, msg, sender, args, kwargs):
This method is called if the name of a received message does not correspond to a method.def _dispatch(self, msg, sender, args, kwargs):
def _loop(self):
def _post_dispatch(self, msg, sender, args, kwargs):
This method is called after a message is dispatched to the appropriate method.def _pre_dispatch(self, msg, sender, args, kwargs):
This method is called between when a message has been received and when it is dispatched to the appropriate method.def filter(self, (msg, sender, args, kwargs)):
This method is passed to self.recv in the dispatcher main loop. Override it to define what messages will be received by this actor.def quit(self, sender):
By default, Dispatcher objects respond to quit messages (presumably from linked actors) by raising StopActor.
class DoNotReply(exceptions.Exception):
Actors incorporating the parley.helpers classes can raise this exception to indicate that no response to the calling actor should be sent.
class StateMachine:
Behaves like Dispatcher, but self.state (along with an underscore) is prepended to message names before they are looked up.def _default(self, msg, sender, args, kwargs):
Default handler, called if (state)__default does not existdef _dispatch(self, msg, sender, args, kwargs):
def _loop(self):
def _state_getattr(self, name, default=None):
def forward_exceptions(lst):
Wraps a function to translate certain exceptions into RemoteExceptions (which will typically be sent as messages to an RPC client). If the list of exceptions is not specified, all exceptions except StopActor are caught in this manner. Usage: @forward_exceptions([KeyError, ValueError]) def foo(): ...
def function_actor(fn, loop=True):
Wraps a function to act as an RPC server. The resulting actor will respond to the "quit" message by exiting and to any other message by calling the function and returning its return value as an RPC reply. If the parameter loop is False, the actor accepts one message and then exits; otherwise, the actor serves forever.