Skip to main content

A Python event framework to decouple code using events.

Project description

Eventlib-py

Release PyPI - Version PyPI - Python Version

Eventlib-py is a small & simple event framework for Python that can be used to decouple your code.

Feature overview:

  • Fast - Pre-computed event chains for fast event emission.
  • Lightweight - No extra dependencies.
  • Asynchronous - Full support for asynchronous event handlers and context managers.
  • Priority Ordering - Control the order in which event handlers are called.
  • Monitoring - Use context managers to monitor event processing.
  • Compatible - Works with almost anything, use: dataclasses, Pydantic or attrs.
import asyncio
import dataclasses

import eventlib


@dataclasses.dataclass
class MyEvent(eventlib.BaseEvent):
    value: str


@eventlib.subscribe()
async def on_my_event(event: MyEvent):
    print(f"Received: {event.value}")


asyncio.run(MyEvent("Hello, world!").emit_async())  # Prints: "Received: Hello, world!"

Usage

Event Inheritance

import eventlib


class MyEvent(eventlib.BaseEvent):
    pass


class SubEvent(MyEvent):
    pass


@eventlib.subscribe()
def on_base_event(event: MyEvent):
    print("Received event", event.__class__.__name__)


SubEvent().emit()  # Prints: "Received event SubEvent"

Priority Ordering

import eventlib


class MyEvent(eventlib.BaseEvent):
    pass


@eventlib.subscribe(priority=-1)
def first():
    print("first")


@eventlib.subscribe()  # default: priority = 0
def second():
    print("second")


@eventlib.subscribe(priority=1)
def third():
    print("third")


MyEvent().emit()  # Prints: "first", "second", "third"

Context Managers

import contextlib
import eventlib


class MyEvent(eventlib.BaseEvent):
    pass


@eventlib.subscribe(priority=-1000)  # Ensure that this is called first
@contextlib.contextmanager
def monitor(event: MyEvent):
    print("Event received")
    try:
        yield
    finally:
        print("Event processed")


@eventlib.subscribe()
def on_event(event: MyEvent):
    print("on_event")


MyEvent().emit()  # Prints: "Event received", "on_event", "Event processed"

Asyncio

import asyncio
import contextlib
import eventlib


class MyEvent(eventlib.BaseEvent):
    pass


@eventlib.subscribe(priority=-1000)  # Ensure that this is called first
@contextlib.asynccontextmanager
async def monitor(event: MyEvent):
    print("Event received")
    try:
        yield
    finally:
        print("Event processed")


@eventlib.subscribe()
async def async_on_event(event: MyEvent):
    print("async_on_event")


@eventlib.subscribe()
def on_event(event: MyEvent):
    print("on_event")


asyncio.run(MyEvent().emit_async())  # Prints: "Event received", "async_on_event", "on_event", "Event processed"

Benchmarks

The benchmark directory contains code to measure the performance of the eventlib-py library and compare it with a hard-coded reference implementation in Python.

Benchmark case_all

The following table shows the overhead of the eventlib-py library in the case_all benchmark. It's a mixed benchmark with all kinds of sync & async event handlers and context managers.

Quantile Hardcoded Time/Event EventLib Time/Event Overhead per Call EventLib Setup
0.50 42.289μs 45.373μs +7% 135.125μs
0.90 43.560μs 46.809μs +10% 143.000μs
0.99 46.658μs 50.048μs +15% 260.393μs

The overhead per call is the additional time that is needed to call the event handlers introduced by the eventlib-py library. The setup time is the additional nonrecurring overhead for subscribing the event handlers in the event system. It shows that in the worst case a 15% overhead per call is introduced. The expected median overhead is around 7% versus hard-coded event handling.

Development

Use poetry to setup the development environment.

poetry install --with=dev
poetry shell

Run the auto-formatter, checks and linter:

black .
isort .
mypy .
pylint .

Run the tests with coverage:

pytest --cov

Contributing

Contributions are welcome.

Please follow the commit convention https://www.conventionalcommits.org/en/v1.0.0/.

License

Dual-licensed under the terms of either the Apache License 2.0 or the MIT license.

SPDX-License-Identifier: (Apache-2.0 OR MIT)

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

eventlib_py-1.1.0.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

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

eventlib_py-1.1.0-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file eventlib_py-1.1.0.tar.gz.

File metadata

  • Download URL: eventlib_py-1.1.0.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for eventlib_py-1.1.0.tar.gz
Algorithm Hash digest
SHA256 f4f74f98094f183f35639b7d79bd307a107e8767ba97527b559e9305dfc815b3
MD5 1049ba0091ae64e6f964daa2d5fda286
BLAKE2b-256 2f64d131260e2e1c0ef5f5a56a57b53af805dd6eeb6903db10ae1e53a159e916

See more details on using hashes here.

Provenance

The following attestation bundles were made for eventlib_py-1.1.0.tar.gz:

Publisher: release.yml on mickare/eventlib-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file eventlib_py-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: eventlib_py-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for eventlib_py-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b1de188025b97e31b01806d0fbf0937aac735a16205016199ee5dfcfb6c54ee7
MD5 dcf61f81d8b72cdd99a7b88b217e9463
BLAKE2b-256 7204c195c34b4193bb87dcef93223ae0373e5f322d8e01fed76582cba3e2ac4a

See more details on using hashes here.

Provenance

The following attestation bundles were made for eventlib_py-1.1.0-py3-none-any.whl:

Publisher: release.yml on mickare/eventlib-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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