pe.tocsin

Tocsin

Simple signal implementation to allow decoupling of implementations by means of emitting and listening for registered signals.

Emitter example:

sig = tocsin.signal('MySignal')
sig.emit('Some MySignal-specific data')

Listener example:

sig = tocsin.signal('MySignal')
sig.listen(lambda name, data: print(
  'name: {}, data: {}\n'.format(name, data)))

As can be seen from the above examples, the signal() function is used to create a new signal object, or obtain an existing one with that name.

pe.tocsin.signal(name)

Create a new signal object with the specified name, or return that object if it already exists.

Parameters:

name (str) – The name of the signal to be returned.

Returns:

A tocsin signal object.

Return type:

signal_object

Once a signal object has been created, it may be listened for and emitted, using the listen() and emit() functions as follows.

pe.tocsin.<signal_object>.listen(cb)

Request notification when a signal is emitted for the signal object on which listen() is invoked. The callback function should have the following signature:

callback(name, data)

where name is the name of the signal, and data is an arbitrary data object to be provided by emit. Since the signal name is provided, the same callback can be used for multiple signals.

Parameters:

cb (function) – The callback function.

pe.tocsin.<signal_object>.emit(data)

Notify all listeners on the signal object, calling their callbacks with the signal name and the provided data. Listeners are called in the order in which they were created.

Parameters:

data (any) – Data to be passed to the callback.

Some inspiration taken from Blinker:

https://blinker.readthedocs.io/