Python中的事件系统 [英] Event system in Python
问题描述
我对大型框架的事件管理器不感兴趣,我宁愿使用一个可以轻松扩展的小型裸机解决方案。
包含答案中提到的各种事件系统:
事件系统最基本的风格是处理程序包,这是观察者格式。基本上,处理程序方法(callable)存储在一个数组中,并且在事件fires时被调用。
- zope.event 显示了它的工作原理(见Lennart的答案)。注意:此示例甚至不支持处理程序参数。
- LongPoke的可调用列表 list 非常简约地实现。
- spassig的EventHook (Michael Foord的事件模式)是一个简单的实现。
- 基本相同,但使用
集合
而不是列表
存储包,并实现__ call __
这两个都是合理的添加。 - PyNotify 在概念上是类似的,并且还提供了变量和条件的其他概念('variable changed event')。 li>
- axel 基本上是一个具有更多与线程,错误处理相关的功能的处理程序,...
这些事件系统的缺点是只能在实际的事件对象(或处理程序列表)上注册处理程序。
所以在注册时,事件已经存在了。
这就是为什么存在第二种类型的事件系统:发布订阅模式。
这里,处理程序不会在事件对象(或处理程序列表)上注册,而是在中央调度程序上注册。通知只与调度员说话。什么听,或什么发布由'信号'确定,这只不过是一个名字(字符串)。
- < a href =https://pythonhosted.org/blinker/ =nofollow noreferrer> blinker 具有一些漂亮的功能,例如基于发件人的自动断开连接和过滤。
- PyPubSub 看起来似乎很简单。
- PyDispatcher 似乎强调了多对多出版物的灵活性等。
- louie 是一个重做的PyDispatcher,提供插件基础设施,包括Twisted和PyQt特定支持。似乎在2016年1月之后失去维护。
- django.dispatch 是一个重写的PyDispatcher,接口更加有限,但性能更高。
- Qt的信号和插槽可从 PyQt 或。它们在同一个线程中使用时作为回调工作,或者作为两个不同线程之间的事件(使用事件循环)。信号和插槽的限制是它们只适用于派生自
QObject
的类的对象。
注意:线程。事件不是上述意义上的事件系统。这是一个线程同步系统,其中一个线程等待直到另一个线程发出Event对象。
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.
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'.
- zope.event shows the bare bones of how this works (see Lennart's answer). Note: this example does not even support handler arguments.
- LongPoke's 'callable list' implementation shows that such an event system can be implemented very minimalistically by subclassing
list
. - spassig's EventHook (Michael Foord's Event Pattern) is a straightforward implementation.
- Josip's Valued Lessons Event class is basically the same, but uses a
set
instead of alist
to store the bag, and implements__call__
which are both reasonable additions. - PyNotify is similar in concept and also provides additional concepts of variables and conditions ('variable changed event').
- axel is basically a bag-of-handlers with more features related to threading, error handling, ...
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).
- blinker has some nifty features such as automatic disconnection and filtering based on sender.
- PyPubSub at first sight seems to be pretty straightforward.
- PyDispatcher seems to emphasize flexibility with regards to many-to-many publication etc.
- louie is a reworked PyDispatcher "providing plugin infrastructure including Twisted and PyQt specific support". It seems to have lost maintenance after January 2016.
- django.dispatch is a rewritten PyDispatcher "with a more limited interface, but higher performance".
- Qt's Signals and Slots are available from PyQt or PySide. 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
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.
这篇关于Python中的事件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!