Base class for all signals Internal attributes: receivers: [ ( (id(receiver), id(sender)), ref(receiver), ref(sender), is_async, ) ] send
| 66 | |
| 67 | |
| 68 | class Signal: |
| 69 | """ |
| 70 | Base class for all signals |
| 71 | |
| 72 | Internal attributes: |
| 73 | |
| 74 | receivers: |
| 75 | [ |
| 76 | ( |
| 77 | (id(receiver), id(sender)), |
| 78 | ref(receiver), |
| 79 | ref(sender), |
| 80 | is_async, |
| 81 | ) |
| 82 | ] |
| 83 | sender_receivers_cache: |
| 84 | WeakKeyDictionary[sender, list[receiver]] |
| 85 | """ |
| 86 | |
| 87 | def __init__(self, use_caching=False): |
| 88 | """ |
| 89 | Create a new signal. |
| 90 | """ |
| 91 | self.receivers = [] |
| 92 | self.lock = threading.Lock() |
| 93 | self.use_caching = use_caching |
| 94 | # For convenience we create empty caches even if they are not used. |
| 95 | # A note about caching: if use_caching is defined, then for each |
| 96 | # distinct sender we cache the receivers that sender has in |
| 97 | # 'sender_receivers_cache'. The cache is cleaned when .connect() or |
| 98 | # .disconnect() is called and populated on send(). |
| 99 | self.sender_receivers_cache = weakref.WeakKeyDictionary() if use_caching else {} |
| 100 | self._dead_receivers = False |
| 101 | |
| 102 | def connect(self, receiver, sender=None, weak=True, dispatch_uid=None): |
| 103 | """ |
| 104 | Connect receiver to sender for signal. |
| 105 | |
| 106 | Arguments: |
| 107 | |
| 108 | receiver |
| 109 | A function or an instance method which is to receive signals. |
| 110 | Receivers must be hashable objects. Receivers can be |
| 111 | asynchronous. |
| 112 | |
| 113 | If weak is True, then receiver must be weak referenceable. |
| 114 | |
| 115 | Receivers must be able to accept keyword arguments. |
| 116 | |
| 117 | If a receiver is connected with a dispatch_uid argument, it |
| 118 | will not be added if another receiver was already connected |
| 119 | with that dispatch_uid. |
| 120 | |
| 121 | sender |
| 122 | The sender to which the receiver should respond. Must either be |
| 123 | a Python object, or None to receive events from any sender. |
| 124 | |
| 125 | weak |
no outgoing calls