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(), andlist_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) -> Nonedef emit_sync(event: str, *args, **kwargs) -> None | asyncio.Taskdef on(event: str, handler, *, priority: int = 0, once: bool = False) -> Nonedef off(event: str, handler = None) -> int(returns removed count; ifhandleris None, removes all)def list_receivers(event: str) -> listdef 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
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0dba16d2bcc8905ecfd81a3aa1a435ec7d5798564b4af9742fad865abcca4a54
|
|
| MD5 |
63da25e3581e793dfd6583123544a9c3
|
|
| BLAKE2b-256 |
59b9e2cda662af1ca05214109151102d4706bc6864ea6575344a5943523556ce
|
Provenance
The following attestation bundles were made for microevents-0.1.0.tar.gz:
Publisher:
publish.yml on pviojo/microevents
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
microevents-0.1.0.tar.gz -
Subject digest:
0dba16d2bcc8905ecfd81a3aa1a435ec7d5798564b4af9742fad865abcca4a54 - Sigstore transparency entry: 653221042
- Sigstore integration time:
-
Permalink:
pviojo/microevents@0ebc14cd29661669ab5aacfa7bca9e7351b642e3 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/pviojo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@0ebc14cd29661669ab5aacfa7bca9e7351b642e3 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb4a4ab948e236d1a1871ee0cc667b3c518ca51088b8c1479069e1bdc647e30b
|
|
| MD5 |
165a858fb2f17a60f5734a044a04fda5
|
|
| BLAKE2b-256 |
e7afdf431ea2e8902148c76b885a33c356f3597d54d003e59d14a471edddb7b2
|
Provenance
The following attestation bundles were made for microevents-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on pviojo/microevents
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
microevents-0.1.0-py3-none-any.whl -
Subject digest:
eb4a4ab948e236d1a1871ee0cc667b3c518ca51088b8c1479069e1bdc647e30b - Sigstore transparency entry: 653221054
- Sigstore integration time:
-
Permalink:
pviojo/microevents@0ebc14cd29661669ab5aacfa7bca9e7351b642e3 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/pviojo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@0ebc14cd29661669ab5aacfa7bca9e7351b642e3 -
Trigger Event:
release
-
Statement type: