我知道 pydispatcher,但必须有其他与 Python 相关的事件包.
I am aware of pydispatcher, but there must be other event-related packages around for Python.
哪些库可用?
我对大型框架中的事件管理器不感兴趣,我宁愿使用可以轻松扩展的小型准系统解决方案.
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.
截至 2021 年 1 月,这些是 PyPI 上可用的与事件相关的软件包,按最近的发布日期排序.
As of January 2021, these are the event-related packages available on PyPI, ordered by most recent release date.
0.3.0:2020 年 11 月4.5.0:2020 年 9 月0.1.31:2020 年 8 月1.0.1:2020 年 6 月0.13.1:2020 年 6 月(测试版)李>2.0:2019 年 9 月4.0.3:2019 年 1 月0.2.3a0:20180.0.5: 20182.1.2:20170.0.7: 20161.4:20152.0.5:20151.0:20120.3.1:2008李>0.3.0: Nov 20204.5.0: Sept 20200.1.31: Aug 20201.0.1: June 20200.13.1: June 2020 (beta)2.0: Sept 20194.0.3: Jan 20190.2.3a0: 20180.0.5: 20182.1.2: 20170.0.7: 20161.4: 20152.0.5: 20151.0: 20120.3.1: 2008有很多库可供选择,使用非常不同的术语(事件、信号、处理程序、方法分派、钩子......).
That's a lot of libraries to choose from, using very different terminology (events, signals, handlers, method dispatch, hooks, ...).
我试图保持对上述软件包的概述,以及此处答案中提到的技术.
I'm trying to keep an overview of the above packages, plus the techniques mentioned in the answers here.
首先,一些术语......
First, some terminology...
事件系统最基本的风格是处理方法包",它是一个观察者模式的简单实现.
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'.
观察者事件系统的缺点是你只能在实际事件上注册处理程序对象(或处理程序列表).因此,在注册时,该事件已经存在.
The disadvantage of Observer 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).
也可能感兴趣:中介者模式.
挂钩"系统通常用于应用程序插件的上下文中.这应用程序包含固定的集成点(钩子),每个插件可能连接到该钩子并执行某些操作.
A 'hook' system is usally used in the context of application plugins. The application contains fixed integration points (hooks), and each plugin may connect to that hook and perform certain actions.
注意:threading.Event 不是事件"系统'在上述意义上.这是一个线程同步系统,其中一个线程一直等待,直到另一个线程发出信号"事件对象.
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.
网络消息传递库也经常使用事件"一词;有时这些在概念上是相似的;有时不是.它们当然可以跨越线程、进程和计算机边界.参见例如pyzmq, pymq,扭曲, 龙卷风, gevent, 小事件.
Network messaging libraries often use the term 'events' too; sometimes these are similar in concept; sometimes not. They can of course traverse thread-, process- and computer boundaries. See e.g. pyzmq, pymq, Twisted, Tornado, gevent, eventlet.
在 Python 中,持有对方法或对象的引用可确保它不会被删除由垃圾收集器.这可能是可取的,但也可能导致内存泄漏:链接的处理程序永远不会清理干净.
In Python, holding a reference to a method or object ensures that it won't get deleted by the garbage collector. This can be desirable, but it can also lead to memory leaks: the linked handlers are never cleaned up.
一些事件系统使用弱引用而不是常规引用来解决这个问题.
Some event systems use weak references instead of regular ones to solve this.
观察者式事件系统:
list 可以非常简约地实现这样的事件系统.set 而不是一个 list 来存储包,并实现 __call__ 这两个都是合理的添加.pydispatch.Dispatcher.list.set instead of a list to store the bag, and implements __call__ which are both reasonable additions.pydispatch.Dispatcher.发布-订阅库:
其他:
pytest 插件使用的钩子系统.QObject 派生的类的对象.pytest plugins.QObject.这篇关于哪些 Python 包提供独立的事件系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何制作一个在 Python 中提供角色的不和谐机器人How to make a discord bot that gives roles in Python?(如何制作一个在 Python 中提供角色的不和谐机器人?)
Discord 机器人没有响应命令Discord bot isn#39;t responding to commands(Discord 机器人没有响应命令)
你能得到“关于我"吗?Discord 机器人的功能?Can you Get the quot;About mequot; feature on Discord bot#39;s? (Discord.py)(你能得到“关于我吗?Discord 机器人的功能?(不和谐.py))
message.channel.id Discord PYmessage.channel.id Discord PY(message.channel.id Discord PY)
如何在 heroku 上托管我的 discord.py 机器人?How do I host my discord.py bot on heroku?(如何在 heroku 上托管我的 discord.py 机器人?)
discord.py - 自动更改角色颜色discord.py - Automaticaly Change an Role Color(discord.py - 自动更改角色颜色)