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 Apache Commons CLI订购帮助选项?   java如何将数据添加到网格视图   java如何在Apache Camel批处理后移动文件?   java如何为日期范围的between子句编写hql查询?   雅加达ee开始Java编程,我应该从哪里开始?   排序Java8+流:检查我的objectinstances的两个字段的列表顺序是否正确   java如何将json转换为Map<String,Object>确保整数为整数   java不能在Spring数据JPA批处理过程中创建TransactionException   java损坏的PDF文件从FTP下载到使用Apache Common Net的设备   java无法使用Spring批处理和Wso2为XML架构命名空间找到Spring NamespaceHandler   java Android ImageView未显示在SherlockFragment中   Maven在构建时出错=无法识别Java路径   java如何使用批处理文件调用关闭处理程序?   java admob广告横幅重叠我的游戏屏幕安卓