Skip to main content

Tiny sync+async event signals for Python.

Project description

microevents

Tiny sync + async event signals for Python with a decorator-based API.

  • @receiver(event: str) decorator to register handlers.
  • emit(event, *args, **kwargs) is async: awaits async handlers and calls sync ones.
  • emit_sync(...) helper to use from non-async code.
  • Programmatic on(), off(), clear(), and list_receivers().
  • Optional EventBus class for isolated buses (tests, plugins, etc.).
  • Handler options: priority (higher runs first), once (auto-unsubscribe after 1 call).
  • Thread-safe registration/dispatch; preserves registration order when priorities tie.
  • MIT licensed. No dependencies.

Installation

pip install microevents

or

uv add microevents

Quick start

from microevents import receiver, emit, emit_sync

@receiver("user_registered")
def welcome(event, user_id, **kw):
    print(f"[{event}] Welcome {user_id}")

@receiver("user_registered")
async def track(event, user_id, **kw):
    import asyncio
    await asyncio.sleep(0.05)
    print(f"[{event}] Tracked {user_id}")

# In async code:
# await emit("user_registered", user_id=42)

# From sync code:
emit_sync("user_registered", user_id=42)

Programmatic registration & EventBus

from microevents import on, off, list_receivers, EventBus

def log(event, *a, **k): print("LOG", event, a, k)

on("ping", log, priority=10)

emit_sync("ping")

bus = EventBus()
bus.on("my_event", log, once=True)

bus.emit_sync("my_event")  # second call does nothing

API

Decorators & functions (module-level global bus)

  • @receiver(event: str, *, priority: int = 0, once: bool = False)
  • async def emit(event: str, *args, **kwargs) -> None
  • def emit_sync(event: str, *args, **kwargs) -> None | asyncio.Task
  • def on(event: str, handler, *, priority: int = 0, once: bool = False) -> None
  • def off(event: str, handler = None) -> int (returns removed count; if handler is None, removes all)
  • def list_receivers(event: str) -> list
  • def clear() -> None

EventBus (isolated)

  • Same API as above, as instance methods.

Errors

Any exception raised by a handler will propagate (fail-fast). If you prefer to isolate failures, wrap your handler or create a small wrapper that catches/logs exceptions.

Testing

pip install -U pytest
pytest

Development

Minimum Python version: 3.8

uv venv --python 3.8 .venv
source .venv/bin/activate

uv sync
uv run pytest

License

MIT © Pablo Viojo

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

microevents-0.1.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

microevents-0.1.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file microevents-0.1.0.tar.gz.

File metadata

  • Download URL: microevents-0.1.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for microevents-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0dba16d2bcc8905ecfd81a3aa1a435ec7d5798564b4af9742fad865abcca4a54
MD5 63da25e3581e793dfd6583123544a9c3
BLAKE2b-256 59b9e2cda662af1ca05214109151102d4706bc6864ea6575344a5943523556ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for microevents-0.1.0.tar.gz:

Publisher: publish.yml on pviojo/microevents

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

File details

Details for the file microevents-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: microevents-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for microevents-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eb4a4ab948e236d1a1871ee0cc667b3c518ca51088b8c1479069e1bdc647e30b
MD5 165a858fb2f17a60f5734a044a04fda5
BLAKE2b-256 e7afdf431ea2e8902148c76b885a33c356f3597d54d003e59d14a471edddb7b2

See more details on using hashes here.

Provenance

The following attestation bundles were made for microevents-0.1.0-py3-none-any.whl:

Publisher: publish.yml on pviojo/microevents

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