Skip to main content

一个 Python 事件总线

项目描述

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

pyeventbus 是 Python 2.7 的发布/订阅事件总线。

  • 简化了python类之间的通信

  • 解耦事件发送者和接收者

  • 执行良好的线程、greenlets、队列和并发进程

  • 避免复杂且容易出错的依赖关系和生命周期问题

  • 让代码更简单

  • 具有高级功能,例如交付线程、工人和产生不同的进程等。

  • 很小(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. 平行:

    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 克隆 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 (4.7 kB 查看哈希)

已上传 source