Skip to main content

ebus - Event bus

Project description

ebus

ebus is a minimalistic event bus for python. It does not need any initialization or sequence for register or raise events.

There are two concepts inside, events and handlers. The subscribers register a handler for a type of event, that can be any type actually, then they recieve a signal when a publisher emits this signal.

ebus accepts to emit the signal if there is no subscriber. However, if you want to handle the events correctly, you should register you handlers before you raise the event.

Installation

The easy way to install package is to use pip:

sudo pip install ebus

Alternatively, you can download it or clone it directly from github and then type:

git clone https://github.com/abisxir/ebus.git
cd ebus
sudo python setup.py install

Usage

The simple way is just like this, define your event, register them using handle decorator and emit your events when it is required:

import ebus

# This can be anything
class MyEvent:
    def __init__(self, message):
        self.message = message

# Handlers can listen to any type
@ebus.handle(MyEvent)
async def handle_my_event(e: MyEvent):
    print(e.message)

# When this call, all registered handlers will run
await ebus.emit_async(MyEvent('My async event happened.'))

If you like to handle it in synchronous way, ebus will take care of that also:

@ebus.handle(MyEvent)
def handle_my_event_sync(e: MyEvent):
    print('Handle the event in sync mode:', e.message)

ebus.emit(MyEvent('My event happened.'))

There is also an event context provided to pass data to the other events or stop the chain:

@ebus.handle(MyEvent)
def handle_first(e: MyEvent, ctx: ebus.EventContext):
    print('I add something to context of ', e.message)
    # Here we can attach any information to context, the next handler will get it.
    ctx['extra'] = 'Extra info can be anything'

@ebus.handle(MyEvent)
def handle_second(e: MyEvent, ctx: ebus.EventContext):
    # Here we get the extra information
    print('There is something for me in ctx [{}].'.format(ctx['extra']))
    # We add another information to context
    ctx['something_else'] = 12

@ebus.handle(MyEvent)
def handle_third(e: MyEvent, ctx: ebus.EventContext):
    print('If something else[{}] is less than 10 I will stop.'.format(ctx['something_else']))
    # It will stop if something_else is less than 10
    if ctx['something_else'] < 10:
        ctx.stop()

@ebus.handle(MyEvent)
def handle_last(e: MyEvent, ctx: ebus.EventContext):
    # We will never reach here to print this, as long as we stop the event chain
    print('Never see this!!!, event process stopped.')

ebus.emit(MyEvent('My event happened.'))

You can also use register_handler to register event handlers:

def handle_my_event(e: MyEvent):
    print(e.message)

# This will register the event handler.
ebus.add_handler(MyEvent, handle_my_event)

# Will run all the handlers registered for this event.
ebus.emit(MyEvent('Test adding handler'))

Also ebus prevents add a handler twice, it just ignores the second one.

def handle_my_event(e: MyEvent):
    print(e.message)

# The first one registers correctly
ebus.add_handler(MyEvent, handle_my_event)

# This will get ignored by ebus
ebus.add_handler(MyEvent, handle_my_event)

ebus.emit(MyEvent('My event happened.'))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ebus-0.1.2.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

ebus-0.1.2-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file ebus-0.1.2.tar.gz.

File metadata

  • Download URL: ebus-0.1.2.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for ebus-0.1.2.tar.gz
Algorithm Hash digest
SHA256 068de83f26394afe0ba7ab7cf03afbffc6101cee3e741c696db34631431d5162
MD5 ef9d7c3fba0e9dede5e4aaa436896cd4
BLAKE2b-256 53f79136d6293f8b5213bbd6706761961e85c5d08b4de341c21b8cb7f70b7721

See more details on using hashes here.

File details

Details for the file ebus-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: ebus-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for ebus-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 08c4f5b7c10d3037527b2a50c9022e63cf9a8287952b052330278611a5bae1d7
MD5 d6fb9579b814815efca27d65f0a69fc4
BLAKE2b-256 89d4674a74f4fbcc84dc9ef90016d9869e3b2c8a1b361225202df89c6598d6d6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page