| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html><head><title>Python: module pydispatch.dispatcher</title> |
| 3 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 4 | </head><body bgcolor="#f0f0f8"> |
| 5 | |
| 6 | <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading"> |
| 7 | <tr bgcolor="#7799ee"> |
| 8 | <td valign=bottom> <br> |
| 9 | <font color="#ffffff" face="helvetica, arial"> <br><big><big><strong><a href="pydispatch.html"><font color="#ffffff">pydispatch</font></a>.dispatcher</strong></big></big></font></td |
| 10 | ><td align=right valign=bottom |
| 11 | ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/home/mcfletch/OpenGL-dev/pydispatcher/pydispatch/dispatcher.py">/home/mcfletch/OpenGL-dev/pydispatcher/pydispatch/dispatcher.py</a></font></td></tr></table> |
| 12 | <p><tt>Multiple-producer-multiple-consumer signal-dispatching<br> |
| 13 | <br> |
| 14 | dispatcher is the core of the PyDispatcher system,<br> |
| 15 | providing the primary API and the core logic for the<br> |
| 16 | system.<br> |
| 17 | <br> |
| 18 | Module attributes of note:<br> |
| 19 | <br> |
| 20 | Any -- Singleton used to signal either "Any Sender" or<br> |
| 21 | "Any Signal". See documentation of the <a href="#_Any">_Any</a> class.<br> |
| 22 | Anonymous -- Singleton used to signal "Anonymous Sender"<br> |
| 23 | See documentation of the <a href="#_Anonymous">_Anonymous</a> class.<br> |
| 24 | <br> |
| 25 | Internal attributes:<br> |
| 26 | WEAKREF_TYPES -- tuple of types/classes which represent<br> |
| 27 | weak references to receivers, and thus must be de-<br> |
| 28 | referenced on retrieval to retrieve the callable<br> |
| 29 | <a href="builtins.html#object">object</a><br> |
| 30 | connections -- { senderkey (id) : { signal : [receivers...]}}<br> |
| 31 | senders -- { senderkey (id) : weakref(sender) }<br> |
| 32 | used for cleaning up sender references on sender<br> |
| 33 | deletion<br> |
| 34 | sendersBack -- { receiverkey (id) : [senderkey (id)...] }<br> |
| 35 | used for cleaning up receiver references on receiver<br> |
| 36 | deletion, (considerably speeds up the cleanup process<br> |
| 37 | vs. the original code.)</tt></p> |
| 38 | <p> |
| 39 | <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 40 | <tr bgcolor="#aa55cc"> |
| 41 | <td colspan=3 valign=bottom> <br> |
| 42 | <font color="#fffff" face="helvetica, arial"><big><strong>Modules</strong></big></font></td></tr> |
| 43 | |
| 44 | <tr><td bgcolor="#aa55cc"><tt> </tt></td><td> </td> |
| 45 | <td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="pydispatch.errors.html">pydispatch.errors</a><br> |
| 46 | </td><td width="25%" valign=top><a href="pydispatch.robustapply.html">pydispatch.robustapply</a><br> |
| 47 | </td><td width="25%" valign=top><a href="pydispatch.saferef.html">pydispatch.saferef</a><br> |
| 48 | </td><td width="25%" valign=top><a href="weakref.html">weakref</a><br> |
| 49 | </td></tr></table></td></tr></table><p> |
| 50 | <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 51 | <tr bgcolor="#ee77aa"> |
| 52 | <td colspan=3 valign=bottom> <br> |
| 53 | <font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> |
| 54 | |
| 55 | <tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> |
| 56 | <td width="100%"><dl> |
| 57 | <dt><font face="helvetica, arial"><a href="builtins.html#object">object</a> |
| 58 | </font></dt><dd> |
| 59 | <dl> |
| 60 | <dt><font face="helvetica, arial"><a href="pydispatch.dispatcher.html#_Parameter">_Parameter</a> |
| 61 | </font></dt><dd> |
| 62 | <dl> |
| 63 | <dt><font face="helvetica, arial"><a href="pydispatch.dispatcher.html#_Anonymous">_Anonymous</a> |
| 64 | </font></dt><dt><font face="helvetica, arial"><a href="pydispatch.dispatcher.html#_Any">_Any</a> |
| 65 | </font></dt></dl> |
| 66 | </dd> |
| 67 | </dl> |
| 68 | </dd> |
| 69 | </dl> |
| 70 | <p> |
| 71 | <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 72 | <tr bgcolor="#ffc8d8"> |
| 73 | <td colspan=3 valign=bottom> <br> |
| 74 | <font color="#000000" face="helvetica, arial"><a name="_Anonymous">class <strong>_Anonymous</strong></a>(<a href="pydispatch.dispatcher.html#_Parameter">_Parameter</a>)</font></td></tr> |
| 75 | |
| 76 | <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> |
| 77 | <td colspan=2><tt>Singleton used to signal "Anonymous Sender"<br> |
| 78 | <br> |
| 79 | The Anonymous <a href="builtins.html#object">object</a> is used to signal that the sender<br> |
| 80 | of a message is not specified (as distinct from being<br> |
| 81 | "any sender"). Registering callbacks for Anonymous<br> |
| 82 | will only receive messages sent without senders. Sending<br> |
| 83 | with anonymous will only send messages to those receivers<br> |
| 84 | registered for Any or Anonymous.<br> |
| 85 | <br> |
| 86 | Note:<br> |
| 87 | The default sender for connect is Any, while the<br> |
| 88 | default sender for send is Anonymous. This has<br> |
| 89 | the effect that if you do not specify any senders<br> |
| 90 | in either function then all messages are routed<br> |
| 91 | as though there was a single sender (Anonymous)<br> |
| 92 | being used everywhere.<br> </tt></td></tr> |
| 93 | <tr><td> </td> |
| 94 | <td width="100%"><dl><dt>Method resolution order:</dt> |
| 95 | <dd><a href="pydispatch.dispatcher.html#_Anonymous">_Anonymous</a></dd> |
| 96 | <dd><a href="pydispatch.dispatcher.html#_Parameter">_Parameter</a></dd> |
| 97 | <dd><a href="builtins.html#object">object</a></dd> |
| 98 | </dl> |
| 99 | <hr> |
| 100 | Methods inherited from <a href="pydispatch.dispatcher.html#_Parameter">_Parameter</a>:<br> |
| 101 | <dl><dt><a name="_Anonymous-__repr__"><strong>__repr__</strong></a>(self)</dt><dd><tt>Return repr(self).</tt></dd></dl> |
| 102 | |
| 103 | <hr> |
| 104 | Data descriptors inherited from <a href="pydispatch.dispatcher.html#_Parameter">_Parameter</a>:<br> |
| 105 | <dl><dt><strong>__dict__</strong></dt> |
| 106 | <dd><tt>dictionary for instance variables (if defined)</tt></dd> |
| 107 | </dl> |
| 108 | <dl><dt><strong>__weakref__</strong></dt> |
| 109 | <dd><tt>list of weak references to the object (if defined)</tt></dd> |
| 110 | </dl> |
| 111 | </td></tr></table><p> |
| 112 | <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 113 | <tr bgcolor="#ffc8d8"> |
| 114 | <td colspan=3 valign=bottom> <br> |
| 115 | <font color="#000000" face="helvetica, arial"><a name="_Any">class <strong>_Any</strong></a>(<a href="pydispatch.dispatcher.html#_Parameter">_Parameter</a>)</font></td></tr> |
| 116 | |
| 117 | <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> |
| 118 | <td colspan=2><tt>Singleton used to signal either "Any Sender" or "Any Signal"<br> |
| 119 | <br> |
| 120 | The Any <a href="builtins.html#object">object</a> can be used with connect, disconnect,<br> |
| 121 | send, or sendExact to signal that the parameter given<br> |
| 122 | Any should react to all senders/signals, not just<br> |
| 123 | a particular sender/signal.<br> </tt></td></tr> |
| 124 | <tr><td> </td> |
| 125 | <td width="100%"><dl><dt>Method resolution order:</dt> |
| 126 | <dd><a href="pydispatch.dispatcher.html#_Any">_Any</a></dd> |
| 127 | <dd><a href="pydispatch.dispatcher.html#_Parameter">_Parameter</a></dd> |
| 128 | <dd><a href="builtins.html#object">object</a></dd> |
| 129 | </dl> |
| 130 | <hr> |
| 131 | Methods inherited from <a href="pydispatch.dispatcher.html#_Parameter">_Parameter</a>:<br> |
| 132 | <dl><dt><a name="_Any-__repr__"><strong>__repr__</strong></a>(self)</dt><dd><tt>Return repr(self).</tt></dd></dl> |
| 133 | |
| 134 | <hr> |
| 135 | Data descriptors inherited from <a href="pydispatch.dispatcher.html#_Parameter">_Parameter</a>:<br> |
| 136 | <dl><dt><strong>__dict__</strong></dt> |
| 137 | <dd><tt>dictionary for instance variables (if defined)</tt></dd> |
| 138 | </dl> |
| 139 | <dl><dt><strong>__weakref__</strong></dt> |
| 140 | <dd><tt>list of weak references to the object (if defined)</tt></dd> |
| 141 | </dl> |
| 142 | </td></tr></table><p> |
| 143 | <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 144 | <tr bgcolor="#ffc8d8"> |
| 145 | <td colspan=3 valign=bottom> <br> |
| 146 | <font color="#000000" face="helvetica, arial"><a name="_Parameter">class <strong>_Parameter</strong></a>(<a href="builtins.html#object">object</a>)</font></td></tr> |
| 147 | |
| 148 | <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> |
| 149 | <td colspan=2><tt>Used to represent default parameter values.<br> </tt></td></tr> |
| 150 | <tr><td> </td> |
| 151 | <td width="100%">Methods defined here:<br> |
| 152 | <dl><dt><a name="_Parameter-__repr__"><strong>__repr__</strong></a>(self)</dt><dd><tt>Return repr(self).</tt></dd></dl> |
| 153 | |
| 154 | <hr> |
| 155 | Data descriptors defined here:<br> |
| 156 | <dl><dt><strong>__dict__</strong></dt> |
| 157 | <dd><tt>dictionary for instance variables (if defined)</tt></dd> |
| 158 | </dl> |
| 159 | <dl><dt><strong>__weakref__</strong></dt> |
| 160 | <dd><tt>list of weak references to the object (if defined)</tt></dd> |
| 161 | </dl> |
| 162 | </td></tr></table></td></tr></table><p> |
| 163 | <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 164 | <tr bgcolor="#eeaa77"> |
| 165 | <td colspan=3 valign=bottom> <br> |
| 166 | <font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr> |
| 167 | |
| 168 | <tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td> |
| 169 | <td width="100%"><dl><dt><a name="-_cleanupConnections"><strong>_cleanupConnections</strong></a>(senderkey, signal)</dt><dd><tt>Delete any empty signals for senderkey. Delete senderkey if empty.</tt></dd></dl> |
| 170 | <dl><dt><a name="-_killBackref"><strong>_killBackref</strong></a>(receiver, senderkey)</dt><dd><tt>Do the actual removal of back reference from receiver to senderkey</tt></dd></dl> |
| 171 | <dl><dt><a name="-_removeBackrefs"><strong>_removeBackrefs</strong></a>(senderkey)</dt><dd><tt>Remove all back-references to this senderkey</tt></dd></dl> |
| 172 | <dl><dt><a name="-_removeOldBackRefs"><strong>_removeOldBackRefs</strong></a>(senderkey, signal, receiver, receivers)</dt><dd><tt>Kill old sendersBack references from receiver<br> |
| 173 | <br> |
| 174 | This guards against multiple registration of the same<br> |
| 175 | receiver for a given signal and sender leaking memory<br> |
| 176 | as old back reference records build up.<br> |
| 177 | <br> |
| 178 | Also removes old receiver instance from receivers</tt></dd></dl> |
| 179 | <dl><dt><a name="-_removeReceiver"><strong>_removeReceiver</strong></a>(receiver)</dt><dd><tt>Remove receiver from connections.</tt></dd></dl> |
| 180 | <dl><dt><a name="-_removeSender"><strong>_removeSender</strong></a>(senderkey)</dt><dd><tt>Remove senderkey from connections.</tt></dd></dl> |
| 181 | <dl><dt><a name="-connect"><strong>connect</strong></a>(receiver, signal=_Any, sender=_Any, weak=True)</dt><dd><tt>Connect receiver to sender for signal<br> |
| 182 | <br> |
| 183 | receiver -- a callable Python <a href="builtins.html#object">object</a> which is to receive<br> |
| 184 | messages/signals/events. Receivers must be hashable<br> |
| 185 | objects.<br> |
| 186 | <br> |
| 187 | if weak is True, then receiver must be weak-referencable<br> |
| 188 | (more precisely saferef.safeRef() must be able to create<br> |
| 189 | a reference to the receiver).<br> |
| 190 | <br> |
| 191 | Receivers are fairly flexible in their specification,<br> |
| 192 | as the machinery in the robustApply module takes care<br> |
| 193 | of most of the details regarding figuring out appropriate<br> |
| 194 | subsets of the sent arguments to apply to a given<br> |
| 195 | receiver.<br> |
| 196 | <br> |
| 197 | Note:<br> |
| 198 | if receiver is itself a weak reference (a callable),<br> |
| 199 | it will be de-referenced by the system's machinery,<br> |
| 200 | so *generally* weak references are not suitable as<br> |
| 201 | receivers, though some use might be found for the<br> |
| 202 | facility whereby a higher-level library passes in<br> |
| 203 | pre-weakrefed receiver references.<br> |
| 204 | <br> |
| 205 | signal -- the signal to which the receiver should respond<br> |
| 206 | <br> |
| 207 | if Any, receiver will receive any signal from the<br> |
| 208 | indicated sender (which might also be Any, but is not<br> |
| 209 | necessarily Any).<br> |
| 210 | <br> |
| 211 | Otherwise must be a hashable Python <a href="builtins.html#object">object</a> other than<br> |
| 212 | None (DispatcherError raised on None).<br> |
| 213 | <br> |
| 214 | sender -- the sender to which the receiver should respond<br> |
| 215 | <br> |
| 216 | if Any, receiver will receive the indicated signals<br> |
| 217 | from any sender.<br> |
| 218 | <br> |
| 219 | if Anonymous, receiver will only receive indicated<br> |
| 220 | signals from send/sendExact which do not specify a<br> |
| 221 | sender, or specify Anonymous explicitly as the sender.<br> |
| 222 | <br> |
| 223 | Otherwise can be any python <a href="builtins.html#object">object</a>.<br> |
| 224 | <br> |
| 225 | weak -- whether to use weak references to the receiver<br> |
| 226 | By default, the module will attempt to use weak<br> |
| 227 | references to the receiver objects. If this parameter<br> |
| 228 | is false, then strong references will be used.<br> |
| 229 | <br> |
| 230 | returns None, may raise DispatcherTypeError</tt></dd></dl> |
| 231 | <dl><dt><a name="-disconnect"><strong>disconnect</strong></a>(receiver, signal=_Any, sender=_Any, weak=True)</dt><dd><tt>Disconnect receiver from sender for signal<br> |
| 232 | <br> |
| 233 | receiver -- the registered receiver to disconnect<br> |
| 234 | signal -- the registered signal to disconnect<br> |
| 235 | sender -- the registered sender to disconnect<br> |
| 236 | weak -- the weakref state to disconnect<br> |
| 237 | <br> |
| 238 | disconnect reverses the process of connect,<br> |
| 239 | the semantics for the individual elements are<br> |
| 240 | logically equivalent to a tuple of<br> |
| 241 | (receiver, signal, sender, weak) used as a key<br> |
| 242 | to be deleted from the internal routing tables.<br> |
| 243 | (The actual process is slightly more complex<br> |
| 244 | but the semantics are basically the same).<br> |
| 245 | <br> |
| 246 | Note:<br> |
| 247 | Using disconnect is not required to cleanup<br> |
| 248 | routing when an <a href="builtins.html#object">object</a> is deleted, the framework<br> |
| 249 | will remove routes for deleted objects<br> |
| 250 | automatically. It's only necessary to disconnect<br> |
| 251 | if you want to stop routing to a live <a href="builtins.html#object">object</a>.<br> |
| 252 | <br> |
| 253 | returns None, may raise DispatcherTypeError or<br> |
| 254 | DispatcherKeyError</tt></dd></dl> |
| 255 | <dl><dt><a name="-getAllReceivers"><strong>getAllReceivers</strong></a>(sender=_Any, signal=_Any)</dt><dd><tt>Get list of all receivers from global tables<br> |
| 256 | <br> |
| 257 | This gets all receivers which should receive<br> |
| 258 | the given signal from sender, each receiver should<br> |
| 259 | be produced only once by the resulting generator</tt></dd></dl> |
| 260 | <dl><dt><a name="-getReceivers"><strong>getReceivers</strong></a>(sender=_Any, signal=_Any)</dt><dd><tt>Get list of receivers from global tables<br> |
| 261 | <br> |
| 262 | This utility function allows you to retrieve the<br> |
| 263 | raw list of receivers from the connections table<br> |
| 264 | for the given sender and signal pair.<br> |
| 265 | <br> |
| 266 | Note:<br> |
| 267 | there is no guarantee that this is the actual list<br> |
| 268 | stored in the connections table, so the value<br> |
| 269 | should be treated as a simple iterable/truth value<br> |
| 270 | rather than, for instance a list to which you<br> |
| 271 | might append new records.<br> |
| 272 | <br> |
| 273 | Normally you would use <a href="#-liveReceivers">liveReceivers</a>( <a href="#-getReceivers">getReceivers</a>( ...))<br> |
| 274 | to retrieve the actual receiver objects as an iterable<br> |
| 275 | <a href="builtins.html#object">object</a>.</tt></dd></dl> |
| 276 | <dl><dt><a name="-liveReceivers"><strong>liveReceivers</strong></a>(receivers)</dt><dd><tt>Filter sequence of receivers to get resolved, live receivers<br> |
| 277 | <br> |
| 278 | This is a generator which will iterate over<br> |
| 279 | the passed sequence, checking for weak references<br> |
| 280 | and resolving them, then returning all live<br> |
| 281 | receivers.</tt></dd></dl> |
| 282 | <dl><dt><a name="-send"><strong>send</strong></a>(signal=_Any, sender=_Anonymous, *arguments, **named)</dt><dd><tt>Send signal from sender to all connected receivers.<br> |
| 283 | <br> |
| 284 | signal -- (hashable) signal value, see connect for details<br> |
| 285 | <br> |
| 286 | sender -- the sender of the signal<br> |
| 287 | <br> |
| 288 | if Any, only receivers registered for Any will receive<br> |
| 289 | the message.<br> |
| 290 | <br> |
| 291 | if Anonymous, only receivers registered to receive<br> |
| 292 | messages from Anonymous or Any will receive the message<br> |
| 293 | <br> |
| 294 | Otherwise can be any python <a href="builtins.html#object">object</a> (normally one<br> |
| 295 | registered with a connect if you actually want<br> |
| 296 | something to occur).<br> |
| 297 | <br> |
| 298 | arguments -- positional arguments which will be passed to<br> |
| 299 | *all* receivers. Note that this may raise TypeErrors<br> |
| 300 | if the receivers do not allow the particular arguments.<br> |
| 301 | Note also that arguments are applied before named<br> |
| 302 | arguments, so they should be used with care.<br> |
| 303 | <br> |
| 304 | named -- named arguments which will be filtered according<br> |
| 305 | to the parameters of the receivers to only provide those<br> |
| 306 | acceptable to the receiver.<br> |
| 307 | <br> |
| 308 | Return a list of tuple pairs [(receiver, response), ... ]<br> |
| 309 | <br> |
| 310 | if any receiver raises an error, the error propagates back<br> |
| 311 | through send, terminating the dispatch loop, so it is quite<br> |
| 312 | possible to not have all receivers called if a raises an<br> |
| 313 | error.</tt></dd></dl> |
| 314 | <dl><dt><a name="-sendExact"><strong>sendExact</strong></a>(signal=_Any, sender=_Anonymous, *arguments, **named)</dt><dd><tt>Send signal only to those receivers registered for exact message<br> |
| 315 | <br> |
| 316 | sendExact allows for avoiding Any/Anonymous registered<br> |
| 317 | handlers, sending only to those receivers explicitly<br> |
| 318 | registered for a particular signal on a particular<br> |
| 319 | sender.</tt></dd></dl> |
| 320 | </td></tr></table><p> |
| 321 | <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 322 | <tr bgcolor="#55aa55"> |
| 323 | <td colspan=3 valign=bottom> <br> |
| 324 | <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr> |
| 325 | |
| 326 | <tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td> |
| 327 | <td width="100%"><strong>Anonymous</strong> = _Anonymous<br> |
| 328 | <strong>Any</strong> = _Any<br> |
| 329 | <strong>WEAKREF_TYPES</strong> = (<class 'weakref.ReferenceType'>, <class 'pydispatch.saferef.BoundMethodWeakref'>)<br> |
| 330 | <strong>__cached__</strong> = '/home/mcfletch/OpenGL-dev/pydispatcher/pydispatch/__pycache__/dispatcher.cpython-310.pyc'<br> |
| 331 | <strong>__file__</strong> = '/home/mcfletch/OpenGL-dev/pydispatcher/pydispatch/dispatcher.py'<br> |
| 332 | <strong>__loader__</strong> = <_frozen_importlib_external.SourceFileLoader object><br> |
| 333 | <strong>__name__</strong> = 'pydispatch.dispatcher'<br> |
| 334 | <strong>__package__</strong> = 'pydispatch'<br> |
| 335 | <strong>__spec__</strong> = ModuleSpec(name='pydispatch.dispatcher', loader=...penGL-dev/pydispatcher/pydispatch/dispatcher.py')<br> |
| 336 | <strong>connections</strong> = {}<br> |
| 337 | <strong>senders</strong> = {}<br> |
| 338 | <strong>sendersBack</strong> = {}</td></tr></table> |
| 339 | </body></html> |