Python中的事件系统 [英] Event system in Python

查看:176
本文介绍了Python中的事件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你使用什么Python的事件系统?我已经意识到 pydispatcher ,但我想知道还能找到还是常用的? / p>

我对大型框架的事件管理器不感兴趣,我宁愿使用一个可以轻松扩展的小型裸机解决方案。

解决方案

包含答案中提到的各种事件系统:



事件系统最基本的风格是处理程序包,这是观察者格式。基本上,处理程序方法(callable)存储在一个数组中,并且在事件fires时被调用。





这些事件系统的缺点是只能在实际的事件对象(或处理程序列表)上注册处理程序。
所以在注册时,事件已经存在了。



这就是为什么存在第二种类型的事件系统:发布订阅模式
这里,处理程序不会在事件对象(或处理程序列表)上注册,而是在中央调度程序上注册。通知只与调度员说话。什么听,或什么发布由'信号'确定,这只不过是一个名字(字符串)。





注意:线程。事件不是上述意义上的事件系统。这是一个线程同步系统,其中一个线程等待直到另一个线程发出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 a list 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆