Whistle

Event Dispatcher for Python

Whistle is a simple tool that allow your application components to communicate with each other by dispatching events and listening to them.

Install Documentation

What is Whistle?

Whistle is a lightweight Event Dispatcher library for Python 3.10+ that enables decoupled application communication through event dispatching and listening.

It's largely inspired from Symfony's EventDispatcher component.

Using an event dispatcher is a great way to write loosely coupled extensible code, having each part only communicate using light events, making your code more modular, testable, and maintainable.


Key Features

  • Synchronous and asynchronous dispatching - Choose the right dispatcher for your use case
  • Priority-based listener execution - Control the order listeners run
  • Event propagation control - Stop event flow when needed
  • Type safety - Prevent mixing sync and async listeners
  • Custom events - Attach domain-specific data to events
  • Zero dependencies - Lightweight and easy to integrate

Quick start

Install the whistle package:

 $ pip install whistle

Create an event dispatcher

from whistle import EventDispatcher

dispatcher = EventDispatcher()

Add a listener to react to events

def on_spectacle_starts(event):
    print('Please turn down your phones!')

dispatcher.add_listener('spectacle.starts', on_spectacle_starts)

Or use the decorator syntax for convenience:

@dispatcher.listen('spectacle.starts')
def on_spectacle_starts(event):
    print('Please turn down your phones!')

Dispatch it!

dispatcher.dispatch('spectacle.starts')
# Output: Please turn down your phones!

License

Whistle and the surrounding material (like this website) is licensed under the Apache License, version 2.0.