Skip to main content
https://travis-ci.org/n89nanda/pyeventbus.svg?branch=master

pyeventbus3 is a publish/subscribe event bus for Python 3. (Fork of pyeventbus for python 2.7)

  • simplifies the communication between python classes

  • decouples event senders and receivers

  • performs well threads, greenlets, queues and concurrent processes

  • avoids complex and error-prone dependencies and life cycle issues

  • makes code simpler

  • has advanced features like delivery threads, workers and spawning different processes, etc.

  • is tiny (3KB archive)

pyeventbus3 in 3 steps:

  1. Define events:

    class MessageEvent:
        # Additional fields and methods if needed
        def __init__(self):
            pass
  2. Prepare subscribers: Declare and annotate your subscribing method, optionally specify a thread mode:

    from pyeventbus3.pyeventbus3 import *
    
    @subscribe(onEvent=MessageEvent)
    def func(self, event):
        # Do something
        pass

    Register your subscriber. For example, if you want to register a class in 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. Post events:

    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()

Modes: pyeventbus can run the subscribing methods in 5 different modes

  1. POSTING:

    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. PARALLEL:

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

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

    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. CONCURRENT:

    Runs the method in a seperate python process::
    
    @subscribe(threadMode = Mode.CONCURRENT, onEvent=MessageEvent)
    def func(self, event):
        # Do something
        pass

Adding pyeventbus to your project:

pip install pyeventbus3

Example:

git clone https://github.com/FlavienVernier/pyeventbus.git

cd pyeventbus

virtualenv venv

source venv/bin/activate

pip install pyeventbus

python example.py

Benchmarks and Performance:

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

Inspiration

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

Metadata

Release files for pyeventbus3 0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyeventbus3 0.10
File Size Uploaded
pyeventbus3-0.10.tar.gz 5.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyeventbus3 0.10
File Interpreter ABI Platform
pyeventbus3-0.10-py3-none-any.whl Python 3 none any Details

Total release size: 16.3 kB

Release files / pyeventbus3-0.10.tar.gz

Download URL pyeventbus3-0.10.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3b16f7e5dd1487e0fc884ebf20dc9075b7c427a1cb31a54abfce999f6fdccfb4
BLAKE2b-256 checksum
How to use checksums
6a89184555ba31c5be2a8f40b484d4c936500c8e4f04203d05fe7fb74fa39ae7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.9

Release files / pyeventbus3-0.10-py3-none-any.whl

Download URL pyeventbus3-0.10-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
160205e503dcf790fe1aec1c34f964dc5c59241384ef855ab6905caeea9fb2cf
BLAKE2b-256 checksum
How to use checksums
d57a5c9e2f8cfbc7f1b448a14c09497deba42d6768fa1e3bae69cd7e4dfb2464
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.9

Release history Release notifications | RSS feed

This release

0.10 This release

2 release files

0.9

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page