kerno.event module

A tiny event hub.

kerno.events will be an instance of EventHub, however you can also use the EventHub class in other circumstances if you want to.

First a library defines an event class to contain event data:

class EventUserLoggedIn:
    def __init__(self, peto):
        self.peto = peto
        # ...and other data that user code might find important.

Elsewhere, user code will import the event class and write a handler:

from example_library import EventUserLoggedIn

def when_user_logs_in(event: EventUserLoggedIn):
    print(event.peto)

Finally, in setup code:

kerno.events.subscribe(EventUserLoggedIn, when_user_logs_in)

Sometimes it is necessary to avoid leaks by unsubscribing:

kerno.events.unsubscribe(EventUserLoggedIn, when_user_logs_in)

One of the advantages of having the event hub as a separate object is that, instead of unsubscribing many handlers, sometimes you can just throw away the hub, replacing its instance.

The library fires the event by doing:

kerno.events.broadcast(EventUserLoggedIn(peto=peto))
class kerno.event.EventHub[source]

Bases: object

A hub for events to be subscribed, fired and removed.

broadcast(event)[source]

Trigger/fire event – execute its subscribers.

The type of event must be an exact match: inheritance is not supported.

subscribe(event_cls: type, function: Callable) Callable[source]

Subscribe a handler function to the event_cls.

unsubscribe(event_cls: type, function: Callable) bool[source]

Remove a function. Return True if it really was subscribed.