Skip to main content

Pydantic-based event management system for Python.

Project description

PyEventManager

Read the Docs!

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyeventmanager-0.6.2.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

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

pyeventmanager-0.6.2-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file pyeventmanager-0.6.2.tar.gz.

File metadata

  • Download URL: pyeventmanager-0.6.2.tar.gz
  • Upload date:
  • Size: 8.9 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

Hashes for pyeventmanager-0.6.2.tar.gz
Algorithm Hash digest
SHA256 14730e490d58df5c89d10f9397701e72c6e80d498bca68b2a16aa69c1fa26e7b
MD5 9ba8c9de50a4f6ef9494ce8ac9ceea92
BLAKE2b-256 36d39aa68d86083cec3e714b60dc269e1ee31d8b092379e35b3502367b46db1a

See more details on using hashes here.

File details

Details for the file pyeventmanager-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: pyeventmanager-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 12.9 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

Hashes for pyeventmanager-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6a13154862532d96b630f8e1671a5886289bc612fc5a4b07d93a4a3b5774ce53
MD5 670d801e27bd4723493a3200c265d3be
BLAKE2b-256 fde4e3ad6687b9c600874407d12413259afdc96011b0f6d36d5672ecb3382991

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