cosapp.core.signal.signal¶
Module defining the Signal class.
Classes
Class that implements a no-op instead of a re-entrant lock. |
|
|
Define a signal by instanciating a |
-
class
cosapp.core.signal.signal.DummyLock[source]¶ Bases:
objectClass that implements a no-op instead of a re-entrant lock.
-
class
cosapp.core.signal.signal.Signal(args: Optional[List[str]] = None, name: Optional[str] = None, threadsafe: bool = False)[source]¶ Bases:
objectDefine a signal by instanciating a
Signalobject, ie.:>>> conf_pre_load = Signal()
Optionaly, you can declare a list of argument names for this signal, ie.:
>>> conf_pre_load = Signal(args=['conf'])
Any callable can be connected to a Signal, it must accept keywords (
**kwargs) and be wrapped in a Slot object, ie.:>>> def yourmodule_conf(conf, **kwargs): ... conf['yourmodule_option'] = 'foo' ...
Connect your function to the signal using
connect():>>> conf_pre_load.connect(Slot(yourmodule_conf))
Emit the signal to call all connected callbacks using
emit():>>> conf = {} >>> conf_pre_load.emit(conf=conf) >>> conf {'yourmodule_option': 'foo'}
Note that you may disconnect a callback from a signal if it is already connected:
>>> conf_pre_load.is_connected(yourmodule_conf) True >>> conf_pre_load.disconnect(yourmodule_conf) >>> conf_pre_load.is_connected(yourmodule_conf) False
-
connect(slot: Union[cosapp.core.signal.slot.Slot, Callable]) → None[source]¶ Connect a callback slot to this signal.
-
disconnect(slot: cosapp.core.signal.slot.Slot) → None[source]¶ Disconnect a slot from a signal if it is connected; else do nothing.
-
emit(**kwargs) → Optional[Any][source]¶ Emit signal, which will execute every connected callback slot, passing keyword arguments.
If a slot returns anything other than None, then
emit()will return that value preventing any other slot from being called.>>> need_something = Signal() >>> def get_something(**kwargs): ... return 'got something' ... >>> def make_something(**kwargs): ... print('I will not be called') ... >>> need_something.connect(get_something) >>> need_something.connect(make_something) >>> need_something.emit() 'got something'
-
is_connected(slot: cosapp.core.signal.slot.Slot) → bool[source]¶ Check if a callback slot is connected to this signal.
-
property
slots¶ Return a list of slots for this signal.
-