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

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.

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.1.tar.gz (30.9 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.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: windmill_lib-0.0.1.tar.gz
  • Upload date:
  • Size: 30.9 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.1.tar.gz
Algorithm Hash digest
SHA256 50115f8214d2d4ae8fcbaaf67052688246fca0ef6087515d93527b3a0b4c0105
MD5 28b038711911b84d70c74ae662526d67
BLAKE2b-256 17ccf86be3c5afb0616dd654bdf840c9b86ecd79d548b2e53ebe514f699e3629

See more details on using hashes here.

File details

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

File metadata

  • Download URL: windmill_lib-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.3 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fe38e4c8886fa8c9c518918c7f1da7f37db479c5b1ebc2c1c9eb5a910ae10422
MD5 6077c2590d2ea579badfd69395af0f3a
BLAKE2b-256 08dbab7f487319dc13e81949edd8ca3295dbd505504b52bcb348da4441a1c629

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