Skip to main content

lightning-fast, high-performance event handle framework

Project description

Tiny-listener

Tiny-listener is a lightweight event framework with Python 3.6+

中文 / English

Requirements

Python 3.6+

Installation

$ pip install tiny-listener

Why use tiny-listener:

  • ✔ Pure Python, tiny install pack
  • ✔ Lighting-fast, based on native coroutine
  • ✔ Easy to use

How it works:

  1. Write your event handler, such as:
@app.on_event("emergency")
def plan_B():
    # do something
    ...

@app.on_event()
def plan_A():
    # do something
    ...
  1. Keep listening (e.g. port, quene ...) until something happen, such as:
async def listen(self):
   while True:
       msg: str = await queue.get()  # msg may be "emergency" or some other status
       self.fire(msg)  # fire event
  1. Tiny-listener will dispatch event automatically:

    • Normally, when listener receive a msg, plan_A will be called by default.
    • If msg is emergency then plan_B will be called instead of plan_A.

Usage

example.py

from tiny_listener import Listener, Event

class App(Listener):
    async def listen(self):
        """listen event"""
        self.fire("Say hi to Alice")
        self.fire("Say hi to Bob")
        self.fire("Say hi to Carol")

        
app = App()


@app.on_event("Say hi to {name}")
async def say_hi(event: Event):
    """handle event"""
    print("Hi,", event.params["name"])

Run application using tiny-listener command:

$ tiny-listener example:app
>>> Hi, Alice
>>> Hi, Bob
>>> Hi, Carol

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

tiny-listener-0.0.10.tar.gz (7.1 kB view hashes)

Uploaded Source

Built Distribution

tiny_listener-0.0.10-py3-none-any.whl (9.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page