Skip to main content

Your application components can communicate by sending and listening to events.

Project description


master develop PyPI Python Licence
Build Status Build Status PyPI

PyEventDispatcher allows your application components to communicate with each other by sending events and listening to them. Inspiration for this library was Symfony's event-dispatcher component.


Library is in very early stage of development. A lot of things can change or might not work as expected, which includes breaking changes.

Easiest example

from pyeventdispatcher import dispatch, Event, register

register("", lambda event: print(f"{}::{}"))
dispatch(Event("", "some data"))
# data


pip install pyeventdispatcher


Any callable can be registered as listener, the only requirements is that it takes one parameter, event.

Below function is simplest example of listener you can define:

def my_listener(event):

Registering global listener

There is several ways of registering your global listener, you can mix styles or keep one across whole application.

register function

from pyeventdispatcher import register

def my_func(event):

register("", my_func)
register("", my_func, -100)

listen decorator

from pyeventdispatcher import listen

@listen("", ("", -100))
def my_func(event):

By extending EventSubscriber class

from pyeventdispatcher import EventSubscriber, register_event_subscribers

class MySubscriber(EventSubscriber):
    EVENTS = {"": "execute_one", "": ("execute_two", -100)}

    def execute_one(event):

    def execute_two(event):

register_event_subscribers() # Register your classes

Local listeners

In most of the cases your application will only need one global registration of listeners that is used across whole application.

Buf if you need, you can initialise as many instances of EventDispatcher as you wish. Everyone of them will have local registry of listeners.

from pyeventdispatcher import EventDispatcher, register

# Register listener in  global registry
register("", lambda event: print("global listener"))

# Initialise instances of local EventDispatcher
py_event_dispatcher_1 = EventDispatcher()
py_event_dispatcher_1.register("", lambda event: print("event dispatcher 1"))

py_event_dispatcher_2 = EventDispatcher()
py_event_dispatcher_2.register("", lambda event: print("event dispatcher 2"))

Registering listeners with execution priority

Listeners are executed in order of priority parameter's value, which by default is set to "0".

You can change priority of registered listener to define in which order it will be executed.

from pyeventdispatcher import register

register("", lambda event: print("second"))
register("", lambda event: print("first "), -100)
# first second

Dispatching an event

When you dispatch your event, every registered listener that listens for occurrence of specified event, will be called with event object as parameter.

Dispatching global event

from pyeventdispatcher import dispatch, Event, register

register("", lambda event: print(

dispatch(Event("", {"id": 1}))

Dispatching local event

from pyeventdispatcher import EventDispatcher, Event, register

register("", lambda event: print(f"{}::global"))

# Initialise separated instance
py_event_dispatcher = EventDispatcher()
py_event_dispatcher.register("", lambda event: print(f"{}::local"))

# Dispatch event to both global and local listeners

# Dispatch event to local listeners only
py_event_dispatcher.dispatch(Event(""), False)

Stopping propagation

Sometimes you might want to stop propagation of event, for that you just have to set event.stop to True,

In example below only first_listener will be executed.

from pyeventdispatcher import register

def first_listener(event):
    event.stop = True

def second_listener(event):

register("", first_listener)
register("", second_listener)
# first_listener

Project details

Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

pyeventdispatcher-0.2.3a0-py3-none-any.whl (6.6 kB view hashes)

Uploaded Python 3

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