python事件总线

pyeventbus的Python项目详细描述


https://travis-ci.org/n89nanda/pyeventbus.svg?branch=master

PyEventBus是Python2.7的发布/订阅事件总线。

  • 简化了python类之间的通信
  • 分离事件发送方和接收方
  • 执行良好的线程、greenlet、队列和并发进程
  • 避免复杂且易出错的依赖关系和生命周期问题
  • 使代码更简单
  • 具有高级功能,如传送线程、工作线程和生成不同进程等。
  • 很小(3KB存档)

PyEventBus分3步:

  1. 定义事件:

    class MessageEvent:
        # Additional fields and methods if needed
        def __init__(self):
            pass
    
  2. 准备订阅服务器:声明并注释订阅方法,或者指定线程模式:

    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)
    
  3. 发布事件:

    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种不同模式下运行订阅方法

  1. 发布:

    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
    
  2. < P>平行:

    Runs the method in a seperate python thread::
    
    @subscribe(threadMode = Mode.PARALLEL, onEvent=MessageEvent)
    def func(self, event):
        # Do something
        pass
    
  3. 绿色:

    Runs the method in a greenlet using gevent library::
    
    @subscribe(threadMode = Mode.GREENLET, onEvent=MessageEvent)
    def func(self, event):
        # Do something
        pass
    
  4. 背景:

    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
    
  1. 并发:

    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 clone https://github.com/n89nanda/pyeventbus.git

cd pyeventbus

virtualenv venv

source venv/bin/activate

pip install pyeventbus

python example.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.

灵感

Inspired by Eventbus from greenrobot: https://github.com/greenrobot/EventBus

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Cassandra DB的javascript查询结果   java定时器任务调度   java在TLS上实现LDAP   为什么在浏览器中滚动会使java小程序闪烁?   未使用轮询器和serviceactivator提取java pubsub消息   java风暴多线程问题   java计算平均成绩   java将字符串添加到另一个类的数组列表中   文件Java路径如何转换为例如InputStream   java更改JComboBox的字体颜色   java inthttp:具有可轮询请求通道的inboundgateway   使用继承在Java中运行swing Base和扩展windows   java ivysettings。xml:添加本地maven路径   java如何将参数自定义视图传递给activity类   java延迟加载无法在Hibernate中使用一对一映射   当文件以“file:/”开头时,Java无法识别该文件   需要java正则表达式帮助,使用反斜杠   片段中的java GWT参数