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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file windmill_lib-0.0.4.tar.gz.
File metadata
- Download URL: windmill_lib-0.0.4.tar.gz
- Upload date:
- Size: 80.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c1a6333371c9c90de1a5f47418021952150ef939e835284a70d05de46123ab0
|
|
| MD5 |
7e634d01da354e528d4f81e22967fcd9
|
|
| BLAKE2b-256 |
3b7d3ccf327aaf0804cf803248a594fad9bf794526e72129adfa29971dfef679
|
File details
Details for the file windmill_lib-0.0.4-py3-none-any.whl.
File metadata
- Download URL: windmill_lib-0.0.4-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
76f0ca5473ac99f075c1e196ba428b6e4e4a6d9b9f436385512d20e4b76a24d1
|
|
| MD5 |
ebdc71383cc4f282a25103db74b0a2f2
|
|
| BLAKE2b-256 |
87e9db512276acd82f6e3b22edc4ca70d006ea464c68605099c09af6202ad22c
|