pydispatch Package¶
pydispatch
Package¶
Multi-consumer multi-producer dispatching mechanism
dispatcher
Module¶
Multiple-producer-multiple-consumer signal-dispatching
dispatcher is the core of the PyDispatcher system, providing the primary API and the core logic for the system.
Module attributes of note:
- Any – Singleton used to signal either “Any Sender” or
- “Any Signal”. See documentation of the _Any class.
- Anonymous – Singleton used to signal “Anonymous Sender”
- See documentation of the _Anonymous class.
- Internal attributes:
- WEAKREF_TYPES – tuple of types/classes which represent
- weak references to receivers, and thus must be de- referenced on retrieval to retrieve the callable object
connections – { senderkey (id) : { signal : [receivers...]}}
- senders – { senderkey (id) : weakref(sender) }
- used for cleaning up sender references on sender deletion
- sendersBack – { receiverkey (id) : [senderkey (id)...] }
- used for cleaning up receiver references on receiver deletion, (considerably speeds up the cleanup process vs. the original code.)
-
connect
(receiver, signal=_Any, sender=_Any, weak=True)[source]¶ Connect receiver to sender for signal
- receiver – a callable Python object which is to receive
messages/signals/events. Receivers must be hashable objects.
if weak is True, then receiver must be weak-referencable (more precisely saferef.safeRef() must be able to create a reference to the receiver).
Receivers are fairly flexible in their specification, as the machinery in the robustApply module takes care of most of the details regarding figuring out appropriate subsets of the sent arguments to apply to a given receiver.
- Note:
- if receiver is itself a weak reference (a callable), it will be de-referenced by the system’s machinery, so generally weak references are not suitable as receivers, though some use might be found for the facility whereby a higher-level library passes in pre-weakrefed receiver references.
signal – the signal to which the receiver should respond
if Any, receiver will receive any signal from the indicated sender (which might also be Any, but is not necessarily Any).
Otherwise must be a hashable Python object other than None (DispatcherError raised on None).
sender – the sender to which the receiver should respond
if Any, receiver will receive the indicated signals from any sender.
if Anonymous, receiver will only receive indicated signals from send/sendExact which do not specify a sender, or specify Anonymous explicitly as the sender.
Otherwise can be any python object.
- weak – whether to use weak references to the receiver
- By default, the module will attempt to use weak references to the receiver objects. If this parameter is false, then strong references will be used.
returns None, may raise DispatcherTypeError
-
disconnect
(receiver, signal=_Any, sender=_Any, weak=True)[source]¶ Disconnect receiver from sender for signal
receiver – the registered receiver to disconnect signal – the registered signal to disconnect sender – the registered sender to disconnect weak – the weakref state to disconnect
disconnect reverses the process of connect, the semantics for the individual elements are logically equivalent to a tuple of (receiver, signal, sender, weak) used as a key to be deleted from the internal routing tables. (The actual process is slightly more complex but the semantics are basically the same).
- Note:
- Using disconnect is not required to cleanup routing when an object is deleted, the framework will remove routes for deleted objects automatically. It’s only necessary to disconnect if you want to stop routing to a live object.
- returns None, may raise DispatcherTypeError or
- DispatcherKeyError
-
getAllReceivers
(sender=_Any, signal=_Any)[source]¶ Get list of all receivers from global tables
This gets all receivers which should receive the given signal from sender, each receiver should be produced only once by the resulting generator
-
getReceivers
(sender=_Any, signal=_Any)[source]¶ Get list of receivers from global tables
This utility function allows you to retrieve the raw list of receivers from the connections table for the given sender and signal pair.
- Note:
- there is no guarantee that this is the actual list stored in the connections table, so the value should be treated as a simple iterable/truth value rather than, for instance a list to which you might append new records.
Normally you would use liveReceivers( getReceivers( ...)) to retrieve the actual receiver objects as an iterable object.
-
liveReceivers
(receivers)[source]¶ Filter sequence of receivers to get resolved, live receivers
This is a generator which will iterate over the passed sequence, checking for weak references and resolving them, then returning all live receivers.
-
send
(signal=_Any, sender=_Anonymous, *arguments, **named)[source]¶ Send signal from sender to all connected receivers.
signal – (hashable) signal value, see connect for details
sender – the sender of the signal
if Any, only receivers registered for Any will receive the message.
if Anonymous, only receivers registered to receive messages from Anonymous or Any will receive the message
Otherwise can be any python object (normally one registered with a connect if you actually want something to occur).
- arguments – positional arguments which will be passed to
- all receivers. Note that this may raise TypeErrors if the receivers do not allow the particular arguments. Note also that arguments are applied before named arguments, so they should be used with care.
- named – named arguments which will be filtered according
- to the parameters of the receivers to only provide those acceptable to the receiver.
Return a list of tuple pairs [(receiver, response), ... ]
if any receiver raises an error, the error propagates back through send, terminating the dispatch loop, so it is quite possible to not have all receivers called if a raises an error.
-
sendExact
(signal=_Any, sender=_Anonymous, *arguments, **named)[source]¶ Send signal only to those receivers registered for exact message
sendExact allows for avoiding Any/Anonymous registered handlers, sending only to those receivers explicitly registered for a particular signal on a particular sender.
errors
Module¶
Error types for dispatcher mechanism
-
exception
DispatcherKeyError
[source]¶ Bases:
exceptions.KeyError
,ars.lib.pydispatch.errors.DispatcherError
Error raised when unknown (sender,signal) set specified
-
exception
DispatcherTypeError
[source]¶ Bases:
exceptions.TypeError
,ars.lib.pydispatch.errors.DispatcherError
Error raised when inappropriate signal-type specified (None)
robust
Module¶
Module implementing error-catching version of send (sendRobust)
-
sendRobust
(signal=_Any, sender=_Anonymous, *arguments, **named)[source]¶ Send signal from sender to all connected receivers catching errors
signal – (hashable) signal value, see connect for details
sender – the sender of the signal
if Any, only receivers registered for Any will receive the message.
if Anonymous, only receivers registered to receive messages from Anonymous or Any will receive the message
Otherwise can be any python object (normally one registered with a connect if you actually want something to occur).
- arguments – positional arguments which will be passed to
- all receivers. Note that this may raise TypeErrors if the receivers do not allow the particular arguments. Note also that arguments are applied before named arguments, so they should be used with care.
- named – named arguments which will be filtered according
- to the parameters of the receivers to only provide those acceptable to the receiver.
Return a list of tuple pairs [(receiver, response), ... ]
if any receiver raises an error (specifically any subclass of Exception), the error instance is returned as the result for that receiver.
robustapply
Module¶
Robust apply mechanism
Provides a function “call”, which can sort out what arguments a given callable object can take, and subset the given arguments to match only those which are acceptable.
saferef
Module¶
Refactored “safe reference” from dispatcher.py
-
class
BoundMethodWeakref
(target, onDelete=None)[source]¶ Bases:
object
‘Safe’ and reusable weak references to instance methods
BoundMethodWeakref objects provide a mechanism for referencing a bound method without requiring that the method object itself (which is normally a transient object) is kept alive. Instead, the BoundMethodWeakref object keeps weak references to both the object and the function which together define the instance method.
- Attributes:
- key – the identity key for the reference, calculated
- by the class’s calculateKey method applied to the target instance method
- deletionMethods – sequence of callable objects taking
- single argument, a reference to this object which will be called when either the target object or target function is garbage collected (i.e. when this object becomes invalid). These are specified as the onDelete parameters of safeRef calls.
weakSelf – weak reference to the target object
weakFunc – weak reference to the target function
- Class Attributes:
- _allInstances – class attribute pointing to all live
- BoundMethodWeakref objects indexed by the class’s calculateKey(target) method applied to the target objects. This weak value dictionary is used to short-circuit creation so that multiple references to the same (object, function) pair produce the same BoundMethodWeakref instance.
Return a weak-reference-like instance for a bound method
- target – the instance-method target for the weak
reference, must have <im_self> and <im_func> attributes and be reconstructable via:
target.<im_func>.__get__( target.<im_self> )which is true of built-in instance methods.
- onDelete – optional callback which will be called
- when this weak reference ceases to be valid (i.e. either the object or the function is garbage collected). Should take a single argument, which will be passed a pointer to this object.
-
safeRef
(target, onDelete=None)[source]¶ Return a safe weak reference to a callable target
- target – the object to be weakly referenced, if it’s a
- bound method reference, will create a BoundMethodWeakref, otherwise creates a simple weakref.
- onDelete – if provided, will have a hard reference stored
- to the callable to be called after the safe reference goes out of scope with the reference object, (either a weakref or a BoundMethodWeakref) as argument.