Pyeventbus: a publish/subscribe event bus

Overview

pyeventbus

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

pyeventbus is a publish/subscribe event bus 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)

pyeventbus 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 pyeventbus 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 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. Post events:

    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.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
    
  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 pyeventbus

Example:

git clone https://github.com/n89nanda/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
You might also like...
Code for the paper
Code for the paper "Unsupervised Contrastive Learning of Sound Event Representations", ICASSP 2021.

Unsupervised Contrastive Learning of Sound Event Representations This repository contains the code for the following paper. If you use this code or pa

Repo for "Event-Stream Representation for Human Gaits Identification Using Deep Neural Networks"

Summary This is the code for the paper Event-Stream Representation for Human Gaits Identification Using Deep Neural Networks by Yanxiang Wang, Xian Zh

Cross-media Structured Common Space for Multimedia Event Extraction (ACL2020)
Cross-media Structured Common Space for Multimedia Event Extraction (ACL2020)

Cross-media Structured Common Space for Multimedia Event Extraction Table of Contents Overview Requirements Data Quickstart Citation Overview The code

CVPRW 2021: How to calibrate your event camera
CVPRW 2021: How to calibrate your event camera

E2Calib: How to Calibrate Your Event Camera This repository contains code that implements video reconstruction from event data for calibration as desc

Repository relating to the CVPR21 paper TimeLens: Event-based Video Frame Interpolation
Repository relating to the CVPR21 paper TimeLens: Event-based Video Frame Interpolation

TimeLens: Event-based Video Frame Interpolation This repository is about the High Speed Event and RGB (HS-ERGB) dataset, used in the 2021 CVPR paper T

An implementation for `Text2Event: Controllable Sequence-to-Structure Generation for End-to-end Event Extraction`

Text2Event An implementation for Text2Event: Controllable Sequence-to-Structure Generation for End-to-end Event Extraction Please contact Yaojie Lu (@

Official PyTorch implementation of N-ImageNet: Towards Robust, Fine-Grained Object Recognition with Event Cameras (ICCV 2021)
Official PyTorch implementation of N-ImageNet: Towards Robust, Fine-Grained Object Recognition with Event Cameras (ICCV 2021)

N-ImageNet: Towards Robust, Fine-Grained Object Recognition with Event Cameras Official PyTorch implementation of N-ImageNet: Towards Robust, Fine-Gra

SurvITE: Learning Heterogeneous Treatment Effects from Time-to-Event Data

SurvITE: Learning Heterogeneous Treatment Effects from Time-to-Event Data SurvITE: Learning Heterogeneous Treatment Effects from Time-to-Event Data Au

Weakly Supervised Dense Event Captioning in Videos, i.e. generating multiple sentence descriptions for a video in a weakly-supervised manner.
Weakly Supervised Dense Event Captioning in Videos, i.e. generating multiple sentence descriptions for a video in a weakly-supervised manner.

WSDEC This is the official repo for our NeurIPS paper Weakly Supervised Dense Event Captioning in Videos. Description Repo directories ./: global conf

Comments
  • Same method name for multiple subscribers bug

    Same method name for multiple subscribers bug

    Please see the code below. To summarize:

    • Define one event
    • Define two subscriber listening for the event above. Each subscriber has a listener method with the name on_event
    • Each of the subscriber classes above defines an instance field, but with unique name (self.something in the first class, self.something2 in the second class)
    • Define another class that posts an event

    Run this scenario and get the error below:

    Exception in thread thread-on_event:
    Traceback (most recent call last):
      File "C:\Anaconda2\envs\python\lib\threading.py", line 801, in __bootstrap_inner
        self.run()
      File "C:\Anaconda2\envs\python\lib\site-packages\pyeventbus\pyeventbus.py", line 112, in run
        self.method(self.subscriber, self.event)
      File "C:/FractureID/projects/python/ui/spectraqc/PyEventBusBug.py", line 16, in on_event
        print (self.something)
    AttributeError: Subscriber2 instance has no attribute 'something'
    
    Exception in thread thread-on_event:
    Traceback (most recent call last):
      File "C:\Anaconda2\envs\python\lib\threading.py", line 801, in __bootstrap_inner
        self.run()
      File "C:\Anaconda2\envs\python\lib\site-packages\pyeventbus\pyeventbus.py", line 112, in run
        self.method(self.subscriber, self.event)
      File "C:/FractureID/projects/python/ui/spectraqc/PyEventBusBug.py", line 26, in on_event
        print (self.something_else)
    AttributeError: Subscriber1 instance has no attribute 'something_else'
    
    

    It complains about the variable in class two not having the attribute in the first class and the other way around.

    If I change on of the on_event to something else like on_event2 then the issue is gone.

    from pyeventbus import *
    
    
    class SomeEvent:
        def __init__(self):
            pass
    
    
    class Subscriber1:
        def __init__(self):
            self.something = 'First subscriber'
            PyBus.Instance().register(self, self.__class__.__name__)
    
        @subscribe(threadMode=Mode.PARALLEL, onEvent=SomeEvent)
        def on_event(self, event):
            print (self.something)
    
    
    class Subscriber2:
        def __init__(self):
            self.something_else = 'Second subscriber'
            PyBus.Instance().register(self, self.__class__.__name__)
    
        @subscribe(threadMode=Mode.PARALLEL, onEvent=SomeEvent)
        def on_event(self, event):
            print (self.something_else)
    
    
    class PyEventBusBug:
    
        def __init__(self):
            Subscriber1()
            Subscriber2()
            PyBus.Instance().post(SomeEvent())
    
    
    if __name__ == "__main__":
        PyEventBusBug()
    
    
    bug 
    opened by ddanny 0
  • Doesn't even start on Windows because 2000 threads is apparently too much

    Doesn't even start on Windows because 2000 threads is apparently too much

      File "C:\Python27\lib\site-packages\pyeventbus\pyeventbus.py", line 116, in subscribe
        bus = PyBus.Instance()
      File "C:\Python27\lib\site-packages\pyeventbus\Singleton.py", line 30, in Instance
        self._instance = self._decorated()
      File "C:\Python27\lib\site-packages\pyeventbus\pyeventbus.py", line 24, in __init__
        for worker in [lambda: self.startWorkers() for i in range(self.num_threads)]: worker()
      File "C:\Python27\lib\site-packages\pyeventbus\pyeventbus.py", line 24, in <lambda>
        for worker in [lambda: self.startWorkers() for i in range(self.num_threads)]: worker()
      File "C:\Python27\lib\site-packages\pyeventbus\pyeventbus.py", line 30, in startWorkers
        worker.start()
      File "C:\Python27\lib\threading.py", line 736, in start
        _start_new_thread(self.__bootstrap, ())
    thread.error: can't start new thread
    

    See also: https://stackoverflow.com/a/1835043/2583080

    bug 
    opened by PawelTroka 4
Releases(0.2)
Deep Q-learning for playing chrome dino game

[PYTORCH] Deep Q-learning for playing Chrome Dino

Viet Nguyen 68 Dec 05, 2022
Covid-19 Test AI (Deep Learning - NNs) Software. Accuracy is the %96.5, loss is the 0.09 :)

Covid-19 Test AI (Deep Learning - NNs) Software I developed a segmentation algorithm to understand whether Covid-19 Test Photos are positive or negati

Emirhan BULUT 28 Dec 04, 2021
Deep Learning Specialization by Andrew Ng, deeplearning.ai.

Deep Learning Specialization on Coursera Master Deep Learning, and Break into AI This is my personal projects for the course. The course covers deep l

Engen 1.5k Jan 07, 2023
Contrastive Learning for Many-to-many Multilingual Neural Machine Translation(mCOLT/mRASP2), ACL2021

Contrastive Learning for Many-to-many Multilingual Neural Machine Translation(mCOLT/mRASP2), ACL2021 The code for training mCOLT/mRASP2, a multilingua

104 Jan 01, 2023
Implements a fake news detection program using classifiers.

Fake news detection Implements a fake news detection program using classifiers for Data Mining course at UoA. Description The project is the categoriz

Apostolos Karvelas 1 Jan 09, 2022
BESS: Balanced Evolutionary Semi-Stacking for Disease Detection via Partially Labeled Imbalanced Tongue Data

Balanced-Evolutionary-Semi-Stacking Code for the paper ''BESS: Balanced Evolutionary Semi-Stacking for Disease Detection via Partially Labeled Imbalan

0 Jan 16, 2022
Official implementation of NeuralFusion: Online Depth Map Fusion in Latent Space

NeuralFusion This is the official implementation of NeuralFusion: Online Depth Map Fusion in Latent Space. We provide code to train the proposed pipel

53 Jan 01, 2023
Code for paper "Vocabulary Learning via Optimal Transport for Neural Machine Translation"

**Codebase and data are uploaded in progress. ** VOLT(-py) is a vocabulary learning codebase that allows researchers and developers to automaticaly ge

416 Jan 09, 2023
Code in PyTorch for the convex combination linear IAF and the Householder Flow, J.M. Tomczak & M. Welling

VAE with Volume-Preserving Flows This is a PyTorch implementation of two volume-preserving flows as described in the following papers: Tomczak, J. M.,

Jakub Tomczak 87 Dec 26, 2022
Official implementation for the paper: Generating Smooth Pose Sequences for Diverse Human Motion Prediction

Generating Smooth Pose Sequences for Diverse Human Motion Prediction This is official implementation for the paper Generating Smooth Pose Sequences fo

Wei Mao 28 Dec 10, 2022
Multi-Template Mouse Brain MRI Atlas (MBMA): both in-vivo and ex-vivo

Multi-template MRI mouse brain atlas (both in vivo and ex vivo) Mouse Brain MRI atlas (both in-vivo and ex-vivo) (repository relocated from the origin

8 Nov 18, 2022
CVPR 2021 - Official code repository for the paper: On Self-Contact and Human Pose.

TUCH This repo is part of our project: On Self-Contact and Human Pose. [Project Page] [Paper] [MPI Project Page] License Software Copyright License fo

Lea Müller 45 Jan 07, 2023
Automatic voice-synthetised summaries of latest research papers on arXiv

PaperWhisperer PaperWhisperer is a Python application that keeps you up-to-date with research papers. How? It retrieves the latest articles from arXiv

Valerio Velardo 124 Dec 20, 2022
Real Time Object Detection and Classification using Yolo Algorithm.

Real time Object detection & Classification using YOLO algorithm. Real Time Object Detection and Classification using Yolo Algorithm. What is Object D

Ketan Chawla 1 Apr 17, 2022
pix2pix in tensorflow.js

pix2pix in tensorflow.js This repo is moved to https://github.com/yining1023/pix2pix_tensorflowjs_lite See a live demo here: https://yining1023.github

Yining Shi 47 Oct 04, 2022
Iranian Cars Detection using Yolov5s, PyTorch

Iranian Cars Detection using Yolov5 Train 1- git clone https://github.com/ultralytics/yolov5 cd yolov5 pip install -r requirements.txt 2- Dataset ../

Nahid Ebrahimian 22 Dec 05, 2022
A Momentumized, Adaptive, Dual Averaged Gradient Method for Stochastic Optimization

MADGRAD Optimization Method A Momentumized, Adaptive, Dual Averaged Gradient Method for Stochastic Optimization pip install madgrad Try it out! A best

Meta Research 774 Dec 31, 2022
Video-Captioning - A machine Learning project to generate captions for video frames indicating the relationship between the objects in the video

Video-Captioning - A machine Learning project to generate captions for video frames indicating the relationship between the objects in the video

1 Jan 23, 2022
Fast, modular reference implementation and easy training of Semantic Segmentation algorithms in PyTorch.

TorchSeg This project aims at providing a fast, modular reference implementation for semantic segmentation models using PyTorch. Highlights Modular De

ycszen 1.4k Jan 02, 2023
Cluttered MNIST Dataset

Cluttered MNIST Dataset A setup script will download MNIST and produce mnist/*.t7 files: luajit download_mnist.lua Example usage: local mnist_clutter

DeepMind 50 Jul 12, 2022