Skip to main content

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

Metadata

Release files for pyeventmanager 0.6.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyeventmanager 0.6.3
File Size Uploaded
pyeventmanager-0.6.3.tar.gz 9.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyeventmanager 0.6.3
File Interpreter ABI Platform
pyeventmanager-0.6.3-py3-none-any.whl Python 3 none any Details

Total release size: 22.2 kB

Release files / pyeventmanager-0.6.3.tar.gz

Download URL pyeventmanager-0.6.3.tar.gz
Size 9.1 kB
Tags Source
SHA-256 checksum
How to use checksums
bd3ab4354e14bdd0a05eee79de240f4bcc53df92ec788e07b6440a6f4f9a37d3
BLAKE2b-256 checksum
How to use checksums
dfad4f1568cd348f2d9a6580f3734fb0f4814d01cd515a9b735e449477025298
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release files / pyeventmanager-0.6.3-py3-none-any.whl

Download URL pyeventmanager-0.6.3-py3-none-any.whl
Size 13.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1cbad28e10fca753ad8186e6f5cf935a8367f213bb37ed6a7c0bd4a4d7f689ec
BLAKE2b-256 checksum
How to use checksums
624d72b85333b54a4a427f5f314921f6d1c431150760b822a01b8e7a6b50118d
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release history Release notifications | RSS feed

This release

0.6.3 This release

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.5.7

2 release files

0.5.6

2 release files

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page