一个 Python 事件总线
项目描述
pyeventbus 是 Python 2.7 的发布/订阅事件总线。
简化了python类之间的通信
解耦事件发送者和接收者
执行良好的线程、greenlets、队列和并发进程
避免复杂且容易出错的依赖关系和生命周期问题
让代码更简单
具有高级功能,例如交付线程、工人和产生不同的进程等。
很小(3KB 存档)
pyeventbus 分 3 步:
定义事件:
class MessageEvent: # Additional fields and methods if needed def __init__(self): pass准备订阅者:声明和注释您的订阅方法,可选择指定线程模式:
from pyeventbus import * @subscribe(onEvent=MessageEvent) def func(self, event): # Do something pass注册您的订阅者。例如,如果你想在 Python 中注册一个类:
from pyeventbus import * class MyClass: def __init__(self): pass def register(self, myclass): PyBus.Instance().register(myclass, self.__class__.__name__) # then during initilization myclass = MyClass() myclass.register(myclass)发布事件:
from pyeventbus import * class MyClass: def __init__(self): pass def register(self, myclass): PyBus.Instance().register(myclass, self.__class__.__name__) def postingAnEvent(self): PyBus.Instance().post(MessageEvent()) myclass = MyClass() myclass.register(myclass) myclass.post()
模式:pyeventbus 可以在 5 种不同的模式下运行订阅方法
发布:
Runs the method in the same thread as posted. For example, if an event is posted from main thread, the subscribing method also runs in the main thread. If an event is posted in a seperate thread, the subscribing method runs in the same seperate method This is the default mode, if no mode has been provided:: @subscribe(threadMode = Mode.POSTING, onEvent=MessageEvent) def func(self, event): # Do something pass平行:
Runs the method in a seperate python thread:: @subscribe(threadMode = Mode.PARALLEL, onEvent=MessageEvent) def func(self, event): # Do something pass格林莱特:
Runs the method in a greenlet using gevent library:: @subscribe(threadMode = Mode.GREENLET, onEvent=MessageEvent) def func(self, event): # Do something pass背景:
Adds the subscribing methods to a queue which is executed by workers:: @subscribe(threadMode = Mode.BACKGROUND, onEvent=MessageEvent) def func(self, event): # Do something pass
同时:
Runs the method in a seperate python process:: @subscribe(threadMode = Mode.CONCURRENT, onEvent=MessageEvent) def func(self, event): # Do something pass
将 pyeventbus 添加到您的项目中:
pip install pyeventbus
例子:
git 克隆 https://github.com/n89nanda/pyeventbus.git cd pyeventbus virtualenv venv 源 venv/bin/激活 pip install pyeventbus 蟒蛇示例.py
基准和性能:
Refer /pyeventbus/tests/benchmarks.txt for performance benchmarks on CPU, I/O and networks heavy tasks. Run /pyeventbus/tests/test.sh to generate the same benchmarks.
灵感
灵感来自 greenrobot 的 Eventbus:https ://github.com/greenrobot/EventBus
项目详情
关
pyeventbus -0.5.tar.gz 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 302262771599f8c6eb04d30bb07631ddad5257ec9adaeebc79e64f603e299465 |
|
| MD5 | 12964bef9b90598178e9497bcc97bc2e |
|
| 布莱克2-256 | 1036533f0338be7155364b334b8f613087f7995d0df6469486c779fa729b1ce5 |