| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>Python Dispatch Package</title> |
| 5 |
<link href="style/sitestyle.css" type="text/css" rel="stylesheet"> |
| 6 |
<meta content="Patrick K. O'Brien" name="author"></head><body> |
| 7 |
<h1>PyDispatcher</h1> |
| 8 |
|
| 9 |
<p class="introduction">PyDispatcher provides the Python programmer |
| 10 |
with a multiple-producer-multiple-consumer signal-registration and |
| 11 |
routing infrastructure for use in multiple contexts. The |
| 12 |
mechanism |
| 13 |
of PyDispatcher started life as a highly rated <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/87056">recipe</a> |
| 14 |
in the <a href="http://aspn.activestate.com/ASPN/Python/Cookbook/">Python |
| 15 |
Cookbook</a>. The <a href="https://github.com/mcfletch/pydispatcher/">project</a> |
| 16 |
aims |
| 17 |
to include various enhancements to the recipe developed during use in |
| 18 |
various applications. It is primarily maintained by <a href="http://www.vrplumber.com">Mike Fletcher</a>. A derivative |
| 19 |
of the project provides the Django web framework's "signal" system. |
| 20 |
</p> |
| 21 |
|
| 22 |
<p>To be more concrete about what PyDispatcher does for you: |
| 23 |
</p> |
| 24 |
|
| 25 |
<ul> |
| 26 |
|
| 27 |
<li>provides a centralized service for delivering messages to |
| 28 |
registered objects (in the local process). It allows you to |
| 29 |
register any number of functions (callable objects) which can receive |
| 30 |
signals from senders.</li> |
| 31 |
<ul> |
| 32 |
<li>registration can be for all senders, particular sending |
| 33 |
objects, or "anonymous" messages (messages where the sender is None) |
| 34 |
</li> |
| 35 |
<li>registration can be for any signal, or particular signals</li> |
| 36 |
<li>a single signal will be delivered to all appropriate registered |
| 37 |
receivers, so that multiple registrations do not interfere with each |
| 38 |
other |
| 39 |
</li> |
| 40 |
</ul> |
| 41 |
<li>there is no requirement for the sender or receiver to be |
| 42 |
dispatcher-aware. Any Python object save the None object can act |
| 43 |
as a sender, and any callable object can act as a receiver. There |
| 44 |
is no need to inherit from a particular class or provide a particular |
| 45 |
interface on the object. |
| 46 |
</li> |
| 47 |
<li>the system uses weak references to receivers wherever possible</li> |
| 48 |
<ul> |
| 49 |
<li>object lifetimes are not affected by PyDispatcher registrations |
| 50 |
(that is, when your object goes away, the registrations related to the |
| 51 |
object also go away). |
| 52 |
</li> |
| 53 |
<li>references to common transient objects (in particular instance |
| 54 |
methods) are stored as compound weak references. |
| 55 |
</li> |
| 56 |
<li>weak references can be disabled on a |
| 57 |
registration-by-registration basis</li> |
| 58 |
</ul> |
| 59 |
<li>allows rich signal types, signals are simply hashable objects |
| 60 |
used to store and retrieve sub-tables, they are otherwise opaque to the |
| 61 |
dispatcher mechanism</li> |
| 62 |
<li>allows sending more information when sending than any particular |
| 63 |
receiver can handle, dispatcher automatically culls those arguments |
| 64 |
which are not appropriate for the particular receiver. This |
| 65 |
allows registering very simple functions dealing with general messages, |
| 66 |
while still allowing natural passing of arguments to higher level |
| 67 |
functions. |
| 68 |
</li> |
| 69 |
</ul> |
| 70 |
|
| 71 |
<p>The dispatcher mechanism is particularly useful when constructing |
| 72 |
Model-View-Controller style applications where it is not desirable to |
| 73 |
have the Model objects aware of the event model.</p> |
| 74 |
|
| 75 |
<h2>Acquisition and Installation</h2> |
| 76 |
|
| 77 |
<p>PyDispatcher is available as a standard Python distutils |
| 78 |
installation package from the Python Package Index (PyPI). To |
| 79 |
install, run: |
| 80 |
</p> |
| 81 |
|
| 82 |
<pre>pip install PyDispatcher</pre> |
| 83 |
|
| 84 |
<p>PyDispatcher does not include any binary packages, so there should |
| 85 |
be no issues in installation. PyDispatcher is maintained on the |
| 86 |
[git project on GitHub](https://github.com/mcfletch/pydispatcher). |
| 87 |
To help develop, check out the project |
| 88 |
like so:</p> |
| 89 |
|
| 90 |
<pre>git clone https://github.com/mcfletch/pydispatcher.git</pre> |
| 91 |
|
| 92 |
<p>You can send a pull request via GitHub</p> |
| 93 |
|
| 94 |
<h2>Documentation</h2> |
| 95 |
|
| 96 |
<p>You can find usage samples in the examples directory of the |
| 97 |
distribution. The dispatcher module's <a href="pydoc/pydispatch.dispatcher.html">reference documentation</a> is |
| 98 |
currently the major source of information regarding usage. |
| 99 |
</p> |
| 100 |
|
| 101 |
<p>PyDispatcher welcomes contributions, suggestions, and feedback from |
| 102 |
users in the pydispatcher-dev <a href="http://lists.sourceforge.net/lists/listinfo/pydispatcher-devel">mailing |
| 103 |
list</a>.</p> |
| 104 |
<h2>Usage</h2> |
| 105 |
<p>To set up a function to receive signals:</p> |
| 106 |
<pre>from pydispatch import dispatcher |
| 107 |
SIGNAL = 'my-first-signal' |
| 108 |
|
| 109 |
def handle_event( sender ): |
| 110 |
"""Simple event handler""" |
| 111 |
print('Signal was sent by', sender) |
| 112 |
dispatcher.connect( handle_event, signal=SIGNAL, sender=dispatcher.Any ) |
| 113 |
</pre> |
| 114 |
<p>The use of the Any object allows the handler to listen for messages |
| 115 |
from any Sender or to listen to Any message being sent. To send |
| 116 |
messages:</p> |
| 117 |
<pre>first_sender = object() |
| 118 |
second_sender = {} |
| 119 |
def main( ): |
| 120 |
dispatcher.send( signal=SIGNAL, sender=first_sender ) |
| 121 |
dispatcher.send( signal=SIGNAL, sender=second_sender ) |
| 122 |
</pre> |
| 123 |
<p>Which causes the following to be printed:</p> |
| 124 |
<pre>Signal was sent by <object object at 0x196a090> |
| 125 |
Signal was sent by {} |
| 126 |
</pre> |
| 127 |
<h3>Handler Functions</h3> |
| 128 |
<p>Handler functions in PyDispatcher are relatively loose in their |
| 129 |
definition. A handler can simply declare the parameters it would |
| 130 |
like to receive and receive only those parameters when the signal is |
| 131 |
sent. The sender can include extra parameters for those handlers |
| 132 |
which require them without worrying about whether a more generic |
| 133 |
handler can accept them:</p> |
| 134 |
<pre>def handle_specific_event( sender, moo ): |
| 135 |
"""Handle a simple event, requiring a "moo" parameter""" |
| 136 |
print 'Specialized event for %(sender)s moo=%(moo)r'%locals() |
| 137 |
dispatcher.connect( handle_specific_event, signal=SIGNAL2, sender=dispatcher.Any ) |
| 138 |
</pre> |
| 139 |
<p>This connection requires that all senders of the particular signal |
| 140 |
send a "moo" parameter, but a handler that listens for all events and |
| 141 |
does not provide a "moo" parameter would silently ignore the sender |
| 142 |
having passed a "moo".</p> |
| 143 |
<p>2 parameters are always available to handler functions if they would like to use them:</p> |
| 144 |
<table style="text-align: left; width: 100%;" border="1" cellpadding="2" cellspacing="2"> |
| 145 |
<tbody> |
| 146 |
<tr> |
| 147 |
<th>Parameter |
| 148 |
|
| 149 |
</th> |
| 150 |
<th>Value |
| 151 |
|
| 152 |
</th> |
| 153 |
</tr> |
| 154 |
<tr> |
| 155 |
<td>sender |
| 156 |
|
| 157 |
</td> |
| 158 |
<td>Object from/for which the event was sent, can be dispatcher.Anonymous for anonymous signals |
| 159 |
|
| 160 |
</td> |
| 161 |
</tr> |
| 162 |
<tr> |
| 163 |
<td>signal |
| 164 |
|
| 165 |
</td> |
| 166 |
<td>Signal object used when sending |
| 167 |
|
| 168 |
</td> |
| 169 |
</tr> |
| 170 |
</tbody> |
| 171 |
</table> |
| 172 |
<p>Positional arguments and named arguments are passed through, but if |
| 173 |
positional arguments are used, they will fill in the parameters of the |
| 174 |
receiver in order and cause conflicts if named parameters are specified |
| 175 |
which match their names. Generally it is advisable to use named |
| 176 |
arguments when defining sending messages.</p> |
| 177 |
<h3>Real World Examples</h3> |
| 178 |
<p>OpenGLContext uses PyDispatcher to provide a link between PyVRML97 |
| 179 |
(model level) and OpenGLContext (the controller and view levels). |
| 180 |
Changes to fields in the model |
| 181 |
<a href="https://github.com/mcfletch/pyvrml97/blob/master/vrml/field.py#L98">send events</a>, |
| 182 |
as do changes to |
| 183 |
<a href="https://github.com/mcfletch/pyvrml97/blob/master/vrml/olist.py#L50">object lists</a>. |
| 184 |
A <a href="https://github.com/mcfletch/pyvrml97/blob/master/vrml/cache.py">cache</a> |
| 185 |
observes these changes and allows the view/model level to |
| 186 |
<a href="https://github.com/mcfletch/openglcontext/blob/master/OpenGLContext/scenegraph/box.py">register dependencies</a> |
| 187 |
on particular fields of particular nodes. The result is that |
| 188 |
rendering code can cache data extensively and have the caches |
| 189 |
consistently invalidated when their data-dependencies are changed.</p> |
| 190 |
|
| 191 |
<h2>Related Software</h2> |
| 192 |
|
| 193 |
<ul> |
| 194 |
|
| 195 |
<li><a href="http://louie.berlios.de/">Louie</a></li> |
| 196 |
<ul> |
| 197 |
<li>Reworked pydispatcher providing plugin infrastructure including |
| 198 |
Twisted and PyQt specific support</li> |
| 199 |
</ul> |
| 200 |
<li><a href="https://code.djangoproject.com/browser/django/trunk/django/dispatch">django.dispatch (Signals)</a></li> |
| 201 |
<ul> |
| 202 |
<li>Rewritten with a more limited interface, but higher |
| 203 |
performance; requires that signals be objects of a given type, and |
| 204 |
eliminates Any registrations. Registrations are on the signal |
| 205 |
objects. |
| 206 |
|
| 207 |
</li> |
| 208 |
</ul> |
| 209 |
|
| 210 |
</ul> |
| 211 |
|
| 212 |
<h2>Release Notes</h2> |
| 213 |
|
| 214 |
<ul> |
| 215 |
<li>Version 2.0.6 |
| 216 |
<ul> |
| 217 |
<li>Packaging fixes, updates to documentation and metadata.</li> |
| 218 |
</ul> |
| 219 |
</li> |
| 220 |
<li>Version 2.0.5</li> |
| 221 |
<ul> |
| 222 |
<li>Python 3.x support via shared code-base (not 2to3). Python 2.x is still the primary development target</li> |
| 223 |
</ul> |
| 224 |
<li>Version 2.0.3</li> |
| 225 |
<ul> |
| 226 |
<li>Support for Python 3.2 (via 2to3) added, Python 2.x is still |
| 227 |
the primary development target |
| 228 |
|
| 229 |
</li> |
| 230 |
</ul> |
| 231 |
<li>Version 2.0.2</li> |
| 232 |
<ul> |
| 233 |
<li>Further packaging fixes. |
| 234 |
|
| 235 |
</li> |
| 236 |
</ul> |
| 237 |
<li>Version 2.0.1 (this version and all previous versions are |
| 238 |
available from the old <a href="http://sourceforge.net/projects/pydispatcher/files/pydispatcher/">SourceForge |
| 239 |
project</a>) |
| 240 |
|
| 241 |
</li> |
| 242 |
<ul> |
| 243 |
<li>Packaging fixes to allow for easy_install based installation</li> |
| 244 |
</ul> |
| 245 |
<li>Version 2.0.0</li> |
| 246 |
<ul> |
| 247 |
<li>Renames the top-level package to "pydispatch" to avoid |
| 248 |
conflicts with common conflicting "dispatch" module.</li> |
| 249 |
</ul> |
| 250 |
<li>Version 1.0.3</li> |
| 251 |
<ul> |
| 252 |
<li>Add "robust" module with single function sendRobust, which |
| 253 |
catches errors during callbacks and returns the error instances instead |
| 254 |
of propagating the error</li> |
| 255 |
<li>Patch bug in SafeRef deletion where traceback module has |
| 256 |
already been deleted by interpreter shutdown</li> |
| 257 |
<li>Patch bug in _removeReceiver where sendersBack has already been |
| 258 |
deleted by interpreter shutdown</li> |
| 259 |
<li>Make SafeRef pre-cache method name to allow for repr after |
| 260 |
cleanup of the method</li> |
| 261 |
</ul> |
| 262 |
<li>Version 1.0.2</li> |
| 263 |
<ul> |
| 264 |
<li>Fixes another memory leak, again wrt the back-reference table |
| 265 |
|
| 266 |
</li> |
| 267 |
</ul> |
| 268 |
<li>Version 1.0.1</li> |
| 269 |
<ul> |
| 270 |
<li>Fixes 2 memory leaks, one regarding the back-reference table |
| 271 |
for receivers, the other being a failure to register all receivers |
| 272 |
beyond the first for deletion</li> |
| 273 |
</ul> |
| 274 |
<li>Version 1.0.0</li> |
| 275 |
<ul> |
| 276 |
<li>Initial SourceForge release with restructured codebase</li> |
| 277 |
</ul> |
| 278 |
</ul> |
| 279 |
|
| 280 |
</body></html> |