Signal
Base class for all signals
Attributes
| Attribute | Type | Description |
|---|---|---|
| receivers | list = [] | Internal list of registered receiver metadata, including lookup keys, weak references to receivers and senders, and asynchronicity flags. |
| lock | threading.Lock = threading.Lock() | A threading lock used to ensure thread-safe access when modifying receivers or clearing the sender cache. |
| use_caching | boolean = False | Boolean flag that determines whether to cache the resolved receivers for each distinct sender to improve performance. |
| sender_receivers_cache | WeakKeyDictionary or dict = {} | A mapping of senders to their resolved receivers, implemented as a WeakKeyDictionary if caching is enabled. |
| _dead_receivers | boolean = False | Internal flag indicating that the receivers list contains dead weak references that need to be cleaned up. |
Constructor
Signature
def Signal(
use_caching: boolean = False
) - > None
Parameters
| Name | Type | Description |
|---|---|---|
| use_caching | boolean = False | If set to True, the signal will cache the receivers for each distinct sender to improve performance on subsequent sends. |
Methods
connect()
@classmethod
def connect(
receiver: callable,
sender: object,
weak: boolean,
dispatch_uid: hashable
) - > null
Connect receiver to sender for signal.
Parameters
| Name | Type | Description |
|---|---|---|
| receiver | callable | A function or instance method that will be called when the signal is emitted; must accept keyword arguments. |
| sender | object | The specific object from which the receiver should respond to events, or None to listen to all senders. |
| weak | boolean | Whether to use weak references to the receiver to allow it to be garbage collected automatically. |
| dispatch_uid | hashable | A unique identifier to prevent the same receiver from being subscribed multiple times. |
Returns
| Type | Description |
|---|---|
null |
disconnect()
@classmethod
def disconnect(
receiver: callable,
sender: object,
dispatch_uid: hashable
) - > boolean
Disconnect receiver from sender for signal.
Parameters
| Name | Type | Description |
|---|---|---|
| receiver | callable | The registered receiver function or method to remove from the signal's dispatch list. |
| sender | object | The registered sender from which the receiver should be disconnected. |
| dispatch_uid | hashable | The unique identifier of the specific receiver instance to disconnect. |
Returns
| Type | Description |
|---|---|
boolean | True if a receiver was successfully disconnected, False otherwise. |
has_listeners()
@classmethod
def has_listeners(
sender: object
) - > boolean
Checks if there are any active receivers currently connected to the signal for a given sender.
Parameters
| Name | Type | Description |
|---|---|---|
| sender | object | The object emitting the signal to check for associated listeners. |
Returns
| Type | Description |
|---|---|
boolean | True if at least one synchronous or asynchronous listener is registered for the sender. |
send()
@classmethod
def send(
sender: object,
**named: dict
) - > list
Send signal from sender to all connected receivers.
Parameters
| Name | Type | Description |
|---|---|---|
| sender | object | The object initiating the signal dispatch. |
| **named | dict | Arbitrary keyword arguments passed directly to every connected receiver. |
Returns
| Type | Description |
|---|---|
list | A list of tuple pairs containing the receiver and its corresponding response value. |
asend()
@classmethod
def asend(
sender: object,
**named: dict
) - > list
Send signal from sender to all connected receivers in async mode.
Parameters
| Name | Type | Description |
|---|---|---|
| sender | object | The object initiating the signal dispatch. |
| **named | dict | Arbitrary keyword arguments passed directly to every connected receiver. |
Returns
| Type | Description |
|---|---|
list | A list of tuple pairs containing the receiver and its corresponding response value, resolved asynchronously. |
send_robust()
@classmethod
def send_robust(
sender: object,
**named: dict
) - > list
Send signal from sender to all connected receivers catching errors.
Parameters
| Name | Type | Description |
|---|---|---|
| sender | object | The object initiating the signal dispatch. |
| **named | dict | Arbitrary keyword arguments passed directly to every connected receiver. |
Returns
| Type | Description |
|---|---|
list | A list of tuple pairs containing the receiver and either its response or the Exception instance if it failed. |
asend_robust()
@classmethod
def asend_robust(
sender: object,
**named: dict
) - > list
Send signal from sender to all connected receivers catching errors.
Parameters
| Name | Type | Description |
|---|---|---|
| sender | object | The object initiating the signal dispatch. |
| **named | dict | Arbitrary keyword arguments passed directly to every connected receiver. |
Returns
| Type | Description |
|---|---|
list | A list of tuple pairs containing the receiver and either its response or the Exception instance if it failed, resolved asynchronously. |