Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Wrapping up the various event systems that are mentioned in the answers here:</p> <p>The most basic style of event system is the 'bag of handler methods', which is a simple implementation of the <a href="http://en.wikipedia.org/wiki/Observer_pattern" rel="noreferrer">Observer pattern</a>. Basically, the handler methods (callables) are stored in an array and are each called when the event 'fires'.</p> <ul> <li><a href="https://pypi.python.org/pypi/zope.event" rel="noreferrer">zope.event</a> shows the bare bones of how this works (see <a href="https://stackoverflow.com/a/1092617/1075152">Lennart's answer</a>). Note: this example does not even support handler arguments.</li> <li><a href="https://stackoverflow.com/a/2022629/1075152">LongPoke's 'callable list'</a> implementation shows that such an event system can be implemented very minimalistically by subclassing <code>list</code>.</li> <li><a href="https://stackoverflow.com/a/1094423/1075152">spassig's EventHook</a> (Michael Foord's Event Pattern) is a straightforward implementation.</li> <li><a href="https://stackoverflow.com/a/1096614/1075152">Josip's Valued Lessons Event class</a> is basically the same, but uses a <code>set</code> instead of a <code>list</code> to store the bag, and implements <code>__call__</code> which are both reasonable additions.</li> <li><a href="http://home.gna.org/py-notify/" rel="noreferrer">PyNotify</a> is similar in concept and also provides additional concepts of variables and conditions ('variable changed event').</li> <li><a href="https://pypi.python.org/pypi/axel" rel="noreferrer">axel</a> is basically a bag-of-handlers with more features related to threading, error handling, ...</li> </ul> <p>The disadvantage of these event systems is that you can only register the handlers on the actual Event object (or handlers list). So at registration time the event already needs to exist.</p> <p>That's why the second style of event systems exists: the <a href="http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern" rel="noreferrer">publish-subscribe pattern</a>. Here, the handlers don't register on an event object (or handler list), but on a central dispatcher. Also the notifiers only talk to the dispatcher. What to listen for, or what to publish is determined by 'signal', which is nothing more than a name (string).</p> <ul> <li><a href="https://pythonhosted.org/blinker/" rel="noreferrer">blinker</a> has some nifty features such as automatic disconnection and filtering based on sender.</li> <li><a href="http://pypubsub.readthedocs.io/en/stable/" rel="noreferrer">PyPubSub</a> at first sight seems to be pretty straightforward.</li> <li><a href="http://pydispatcher.sourceforge.net/" rel="noreferrer">PyDispatcher</a> seems to emphasize flexibility with regards to many-to-many publication etc.</li> <li><a href="https://github.com/11craft/louie" rel="noreferrer">louie</a> is a reworked PyDispatcher "providing plugin infrastructure including Twisted and PyQt specific support". It seems to have lost maintenance after January 2016.</li> <li><a href="https://code.djangoproject.com/browser/django/trunk/django/dispatch" rel="noreferrer">django.dispatch</a> is a rewritten PyDispatcher "with a more limited interface, but higher performance".</li> <li>Qt's Signals and Slots are available from <a href="http://pyqt.sourceforge.net/Docs/PyQt4/new_style_signals_slots.html" rel="noreferrer">PyQt</a> or <a href="https://wiki.qt.io/Signals_and_Slots_in_PySide" rel="noreferrer">PySide</a>. They work as callback when used in the same thread, or as events (using an event loop) between two different threads. Signals and Slots have the limitation that they only work in objects of classes that derive from <code>QObject</code>.</li> </ul> <p>Note: <a href="https://docs.python.org/3.5/library/threading.html#event-objects" rel="noreferrer">threading.Event</a> is not an 'event system' in the above sense. It's a thread synchronization system where one thread waits until another thread 'signals' the Event object.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload