What event system for Python do you use? I'm already aware of pydispatcher, but I was wondering what else can be found, or is commonly used?
I'm not interested in event managers that are part of large frameworks, I'd rather use a small bare-bones solution that I can easily extend.
I use zope.event. It's the most bare bones you can imagine. :-) In fact, here is the complete source code:
Note that you can't send messages between processes, for example. It's not a messaging system, just an event system, nothing more, nothing less.
Here's another module for consideration. It seems a viable choice for more demanding applications.
I made a variation of Longpoke's minimalistic approach that also ensures the signatures for both callees and callers:
You may have a look at pymitter (pypi). Its a small single-file (~250 loc) approach "providing namespaces, wildcards and TTL".
Here's a basic example:
Wrapping up the various event systems that are mentioned in the answers here:
The most basic style of event system is the 'bag of handler methods', which is a simple implementation of the Observer pattern. Basically, the handler methods (callables) are stored in an array and are each called when the event 'fires'.
list
.set
instead of alist
to store the bag, and implements__call__
which are both reasonable additions.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.
That's why the second style of event systems exists: the publish-subscribe pattern. 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).
QObject
.Note: threading.Event 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.
Note: not yet included above are pypydispatcher, python-dispatch and the 'hook system' of pluggy might be of interest as well.
I created an
EventManager
class (code at the end). The syntax is the following:Here is an Example:
Output:
EventManger Code: