Skip to main content

A tiny library that offers support for event handling and messaging streams between multiple consumers.

Project description

Windmill

Handling events on Python in a soft way


What is Windmill?

This project is a Python library that offers a more soft and minimalist way to handle events in your Python application. The main idea of Windmill is to provide a decoupled way to communicate a lot of parts of your code about events without creating any direct links or dependencies between isolated regions of code.


The Features

This project currently supports the following features:

  • Async event publishing
  • Sync and async handlers (listeners)
  • Priority level for events
  • "once" execution for events
  • Simple listeners subscription API and decorators

How to install

You can install the library through pip install command:

pip install windmill-lib

How to use

Here is a little example of how to use this library in your project:

# Here we import the library
from windmill.event_bus import EventBus,  Event

# It creates an instance of EventBus
bus = EventBus()

# Here we define an event listener
# We are saying that when the 'greet' event is emitted, then executes something. This listener receives the emitted event instance as parameter.
@bus.on('greet')
def hello(event: Event):
    print(f"Hello world, {event.payload}!")

# Here we publish the 'greet' event with the 'Bob' as payload (data)
bus.publish('greet', 'Bob')

The expected output for the code above is:

Hello world, Bob!

Async support

You can also create async listeners and publish events using asynchronous mode:

import asyncio

@bus.on('greet')
async def hello_async(event: Event):
    await asyncio.sleep(1.0)
    print(f"Hello world, {event.payload}!")

bus.publish('greet', 'Max')

Publishing with asynchronous functions:

async def main():
    await bus.publish_async('greet', 'Caroline')
    await bus.publish_async('greet', 'Daniel')

asyncio.run(main())

Priority parameter

Listeners can also have priority level and may be executed in different orders according to its priority. Higher priority grants earlier execution.

@bus.on('greet', priority=2)
def hello_first(event: Event):
    print(f"Hello world, {event.payload}! I am first.")

@bus.on('greet', priority=1)
def hello_second(event: Event):
    print(f"Hello world, {event.payload}! I am second.")

bus.publish('greet', 'Max')

This will generate the following output:

Hello world, Max! I am first.
Hello world, Max! I am second.

"once" parameter

You can setup a listener to be executed only once before its deletion:

@bus.on('greet', once=True)
def hello_once(event: Event):
    print(f"Hello, {event.payload}! Executed only once and nevermore.")

bus.publish('greet', 'Max')
bus.publish('greet', 'Bob')

This will result in the following output:

Hello, Max! Executed only once and nevermore.

"static" parameter

You can also create static listeners. These listeners save a state with the last payload received. If the current payload is equivalent to the last payload, the listener will not be executed.

bus = EventBus()

@bus.on('greet', static=True)
def hello(event: Event):
    print(f"Hello, {event.payload}!")

bus.publish('greet', 'Max')
bus.publish('greet', 'Max') # this shouldn't be printed.
bus.publish('greet', 'Bob')

The output will be:

Hello, Max!
Hello, Bob!

Todos:

  • Wildcard topics
  • Retry/Backoff and DLQ
  • Write unit tests
  • Roles and permissions
  • Payload validation
  • Data persistence (json, db, etc.)
  • Metrics and tracing system
  • Support for object-oriented listeners

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

windmill_lib-0.0.3.tar.gz (33.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

windmill_lib-0.0.3-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file windmill_lib-0.0.3.tar.gz.

File metadata

  • Download URL: windmill_lib-0.0.3.tar.gz
  • Upload date:
  • Size: 33.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for windmill_lib-0.0.3.tar.gz
Algorithm Hash digest
SHA256 54639b9a4dfd5529ea28ae9364e8520dc70242b8b4e410e2930918d10729960e
MD5 fadd7fce23cabe1c1c160a0e34a4c42d
BLAKE2b-256 da6db2acf1faa48d16f6fde4d5d58f41d4123708256ce3e3e707ffba8c9774c4

See more details on using hashes here.

File details

Details for the file windmill_lib-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: windmill_lib-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for windmill_lib-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 90149462c5ab1bc6a108e93ac725dbd8014bb985c75dee9d2c6590b88d3ba18b
MD5 1ab9f5efd47b6c7871d958538e92304e
BLAKE2b-256 616f0e4c356e08cf314f7641f2f56b76e2f0839899717e2708e0027051f3d61e

See more details on using hashes here.

Supported by

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