Python事件总线

pyeventbus3的Python项目详细描述


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

pyeventbus3是python3的发布/订阅事件总线。(python2.7的pyeventbus分支)

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

pyeventbus3分3步:

  1. 定义事件:

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

    ^{pr2}$

    注册您的订户。例如,如果要在Python中注册类:

    from pyeventbus3.pyeventbus3 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 pyeventbus3.pyeventbus3 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.postingAnEvent()
    

模式: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. 平行:

    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
    
    There is 10 workers by default, see exemple to modify this number.
    
  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 pyeventbus3

示例:

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

Performance comparison between all the modes with Python and Cython
alternate text

灵感

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

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

推荐PyPI第三方库


热门话题
java如何序列化数字?   java如何用我的Mainclass扩展ListActivity类和Fragment类?   JavaJersey、Jackson和JAXRS发布了多种JSON格式   java如何使用JavaFXKeyCombination覆盖系统默认的键盘快捷键,如Ctrl+C、Ctrl+V?   java Jersey类路径扫描示例Jersey。配置。服务器供应商。类路径   java什么样的数据结构可以作为一个数组,但在同一索引下给定多个值时会自动增长到第二维度?   java如何序列化非持久实体中的嵌套PersistentEntityResource   协议缓冲区我可以让protoc在Java中生成int数组吗?   在GregorianCalendar ArrayList Java中添加日期元素   从html模板动态生成pdf文件并用java生成目录   java Gridgain 6.5.5开源多个节点速度较慢。。?   java如何检查数组中所有整数的不相等性?   java在Eclipse中,如何多次运行JUnit测试用例   java侦听器不能处理特定的片段   java不是一个声明?(蓝色J)   找不到Attributer类型的PersonId的java定义