zengine.dispatch package

zengine.dispatch.dispatcher module

class zengine.dispatch.dispatcher.Signal(providing_args=None, use_caching=False)[source]

Bases: object

Base class for all signals

Internal attributes:

receivers
{ receiverkey (id) : weakref(receiver) }
connect(receiver, sender=None, weak=True, dispatch_uid=None)[source]

Connect receiver to sender for signal.

Parameters:
  • receiver

    A function or an instance method which is to receive signals. Receivers must be hashable objects.

    If weak is True, then receiver must be weak referenceable.

    Receivers must be able to accept keyword arguments.

    If a receiver is connected with a dispatch_uid argument, it will not be added if another receiver was already connected with that dispatch_uid.

  • sender – The sender to which the receiver should respond. Must either be of type Signal, or None to receive events from any sender.
  • 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.
  • dispatch_uid – An identifier used to uniquely identify a particular instance of a receiver. This will usually be a string, though it may be anything hashable.
disconnect(receiver=None, sender=None, dispatch_uid=None)[source]

Disconnect receiver from sender for signal.

If weak references are used, disconnect need not be called. The receiver will be remove from dispatch automatically.

Parameters:
  • receiver – The registered receiver to disconnect. May be none if dispatch_uid is specified.
  • sender – The registered sender to disconnect
  • dispatch_uid – the unique identifier of the receiver to disconnect
has_listeners(sender=None)[source]
send(sender, **named)[source]

Send signal from sender to all connected receivers.

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.

Parameters:
  • sender – The sender of the signal Either a specific object or None.
  • named – Named arguments which will be passed to receivers.

Returns a list of tuple pairs [(receiver, response), ... ].

send_robust(sender, **named)[source]

Send signal from sender to all connected receivers catching errors.

Parameters:
  • sender – The sender of the signal. Can be any python object (normally one registered with a connect if you actually want something to occur).
  • named – Named arguments which will be passed to receivers. These arguments must be a subset of the argument names defined in providing_args.

Return a list of tuple pairs [(receiver, response), ... ]. May raise DispatcherKeyError.

If any receiver raises an error (specifically any subclass of Exception), the error instance is returned as the result for that receiver. The traceback is always attached to the error at __traceback__.

zengine.dispatch.dispatcher.receiver(signal, **kwargs)[source]

A decorator for connecting receivers to signals. Used by passing in the signal (or list of signals) and keyword arguments to connect:

@receiver(post_save, sender=MyModel)
def signal_receiver(sender, **kwargs):
    ...

@receiver([post_save, post_delete], sender=MyModel)
def signals_receiver(sender, **kwargs):
    ...

zengine.dispatch.weakref_backports module

weakref_backports is a partial backport of the weakref module for python versions below 3.4.

Copyright (C) 2013 Python Software Foundation, see license.python for details.

The following changes were made to the original sources during backporting:

  • Added self to super calls.
  • Removed from None when raising exceptions.
class zengine.dispatch.weakref_backports.WeakMethod[source]

Bases: weakref

A custom weakref.ref subclass which simulates a weak reference to a bound method, working around the lifetime problem of bound methods.

__hash__