A lightweight event framework
Documentation: https://molto0504.github.io/tiny-listener
Source Code: https://github.com/molto0504/tiny-listener
Introduction
Tiny-listener is a lightweight and flexible event framework.
Requirements
Python 3.8+
Installation
$ pip install tiny-listener
Why use tiny-listener
- ✔ Pure Python.
- ✔ Lightning-fast, based on native coroutine.
- ✔ 100% test coverage.
Example
Consider we want to write a program keep listening message received from somewhere (e.g. http request or message queue), when message arrived, we want to execute a series of operations in a certain order, e.g.
- step 1: save user data to database
- step 2: send email to user
In the development process, we often encounter such problems:
- the source of step 1 and step 2 may be different
- step 2 may depend on the result of step 1, that is to say, step 2 may need to wait for step 1 to complete
- there are many messages, many events, and it is not easy to maintain
Tiny-listener may help you solve these problems:
Create a file example.py with:
from tiny_listener import Event, Listener, Param
class App(Listener):
async def listen(self):
ctx = self.new_ctx()
ctx.trigger_event("step 2: send email to alice@tl.com")
ctx.trigger_event("step 1: save Alice's data to database", data={"age": 35})
app = App()
@app.on_event("step 1: save {username}'s data to database")
async def step_1(event: Event, username: Param):
age = event.data["age"]
print(f"Step-1: Save data done!, {username=}, {age=}")
@app.on_event("step 2: send email to {email}")
async def step_2(event: Event, email: Param):
await event.wait_event_done("step_1")
print(f"Step-2: Send email done!, {email=}")
Run it:
$ tiny-listener example:app
>>> Save data done!, username='Alice', age=35
>>> Send email done!, email='alice@tl.com'
How it works
- Create your own Listener and listen something(e.g. port, queue ...):
from tiny_listener import Event, Listener, Param
class App(Listener):
async def listen(self):
ctx = self.new_ctx()
ctx.trigger_event("step 2: send email to alice@tl.com")
ctx.trigger_event("step 1: save Alice's data to database", data={"age": 35})
- Add event handler to your listener:
app = App()
@app.on_event("step 1: save {username}'s data to database")
async def step_1(event: Event, username: Param):
age = event.data["age"]
print(f"Step-1: Save data done!, {username=}, {age=}")
@app.on_event("step 2: send email to {email}")
async def step_2(event: Event, email: Param):
await event.wait_event_done("step_1")
print(f"Step-2: Send email done!, {email=}")
- Run listener with command:
$ tiny-listener example:app
- Tiny-listener will dispatch every event automatically:
>>> Step-1: Save data done!, username='Alice', age=35
>>> Step-2: Send email done!, email='alice@tl.com'
Metadata
Release files for tiny-listener 1.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tiny-listener-1.2.1.tar.gz | 11.4 kB | Without provenance | Details for tiny-listener-1.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tiny_listener-1.2.1-py3-none-any.whl | Python 3 | none | any | Details for tiny_listener-1.2.1-py3-none-any.whl |
Total release size: 40.1 kB
Release files / tiny-listener-1.2.1.tar.gz
| Download URL | tiny-listener-1.2.1.tar.gz |
|---|---|
| Size | 11.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e2f448a6f9ffe98a4a7ec0faccc33d82452074337799a926ca93bd0931bc6c70
|
|
BLAKE2b-256 checksum How to use checksums |
be2bc1740f15784dbbe8c6e345a74ea1e7f3ed6c881b28616f49403584f2bd9b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.14
|
Release files / tiny_listener-1.2.1-py3-none-any.whl
| Download URL | tiny_listener-1.2.1-py3-none-any.whl |
|---|---|
| Size | 28.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fd71e5a0eab6664608e9cb485cbfda697622188c08bddfa1e411bac33f93352e
|
|
BLAKE2b-256 checksum How to use checksums |
6e2e42fd25f9073c9142b559b04c7076257fc17785e0123e1cae515ab10a7eb2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.14
|
Security for tiny-listener 1.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.