Pydantic-based event management system for Python.
Project description
PyEventManager
PyEventManager is a simple event-based routing package allowing code to be structured in a pseudo-decentralized manner.
Instead of calling functions directly, you can emit an event and have one or more functions (listeners) configured to listen on that event execute. These listeners can currently be run either in a new thread or a new process, allowing the creation of background tasks for long-running jobs off an API event, for example.
Wrapped listeners return a Future that can be waited on to recieve the response(s) back from the listener.
There are multiple execution options when registering a listener:
- Simple: Execute the function (listener) when the specified event is emitted. Execution will occur in a new thread.
- Batch: Batch up the data from mulitple event emissions until one of the conditions is met, then execute the function with all of the received data. Execution and batching will occur in a new thread.
- Scheduled: Run a function on an interval with no inputs. Execution occurs in a new process.
Todo
- Add tests
- Add support for external data stores (redis, rabbitmq?, etc.) for persistence of event data / batching
Installation
Install via pip
pip install pyeventmanager
Usage
Events
from event_manager import EventModel
class MyEvent(EventModel):
__event_name__ = "somecategory.event"
stuff: str
Simple Listener
from event_manager import EventManager
# Register a function to handle a specific event by name
@EventManager.on(event="somecategory.event")
def handle_some_event(data: MyDataType):
...
# Register a function to handle a specific event by model
@EventManager.on(event=MyEvent)
def handle_some_event_model(data: MyEvent):
...
# Register a function to handle all events in the system
@EventManager.on(event="*")
def handle_all_events(data: Any):
...
# Register a function to handle all events for a category using wildcard
@EventManager.on(event="somecategory.*")
def handle_all_somecategory_events(data: Any):
...
# Emit an event
EventManager.emit(MyEvent(...))
# Emit an event, wait for jobs to finish, and get the results
from concurrent.futures import wait
futures = EventManager.emit(MyEvent(...))
wait(futures)
results = [f.result() for f in futures]
Batch Listener
from event_manager import EventManager
# Batch all data for `MyEvent` until no new events occur for 60 seconds
@EventManager.on_batch(event=MyEvent, batch_idle_window=60)
def handle_some_event_batch(data: list[MyEvent]):
...
# Batch data until 30 seconds pass, or 20 events come through, whichever happends first
@EventManager.on_batch(event=MyEvent, batch_count=20, batch_window=30)
def handle_some_event_batch(data: list[MyEvent]):
...
# Batch all data for `MyEvent` and `MySecondEvent` until no new events occur for 60 seconds
@EventManager.on_batch(event=[MyEvent, MySecondEvent], batch_idle_window=60)
def handle_some_event_batch(data: list[MyEvent|MySecondEvent]):
...
Scheduled Listener
Interval is defined using a datetime.timedelta object.
from datetime import timedelta
from event_manager import EventManager
# Schedule a function to be run daily
@EventManager.schedule(interval=timedelta(days=1))
def run_daily():
...
# Schedule a function to be run hourly
@EventManager.schedule(interval=timedelta(hours=1))
def run_hourly():
...
API Documentation
Project details
Release history Release notifications | RSS feed
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 pyeventmanager-0.6.3.tar.gz.
File metadata
- Download URL: pyeventmanager-0.6.3.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd3ab4354e14bdd0a05eee79de240f4bcc53df92ec788e07b6440a6f4f9a37d3
|
|
| MD5 |
3513bd1cdb24a53ec42ee5f3f30920ee
|
|
| BLAKE2b-256 |
dfad4f1568cd348f2d9a6580f3734fb0f4814d01cd515a9b735e449477025298
|
File details
Details for the file pyeventmanager-0.6.3-py3-none-any.whl.
File metadata
- Download URL: pyeventmanager-0.6.3-py3-none-any.whl
- Upload date:
- Size: 13.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1cbad28e10fca753ad8186e6f5cf935a8367f213bb37ed6a7c0bd4a4d7f689ec
|
|
| MD5 |
2f3cc7d95dfe10740a62689af476f6a5
|
|
| BLAKE2b-256 |
624d72b85333b54a4a427f5f314921f6d1c431150760b822a01b8e7a6b50118d
|