Event
The Event class is a lightweight multicast delegate that supports both synchronous and asynchronous listeners.
This pattern is useful when you want to notify multiple callbacks of a single event, such as in plugin systems, GUI toolkits, or loosely coupled applications. It provides an elegant alternative to more verbose observer implementations or string-based event emitters.
Overview
Add a listener with
+=(function, method, or coroutine)Remove a listener with
-=Trigger the event with
event(args...)(non-blocking)Use
await event.call_async(...)to await all listeners
Example Usage
from pattern_kit import Event
def on_document_loaded(doc):
print("Sync listener:", doc)
async def on_document_async(doc):
print("Async listener:", doc)
on_new_document = Event()
on_new_document += on_document_loaded
on_new_document += on_document_async
# Fire-and-forget (async listeners will run in background)
on_new_document("report.pdf")
# Fully awaited (awaits async listeners)
await on_new_document.call_async("report.pdf")
API Reference
- class pattern_kit.behavioral.event.Event[source]
Bases:
objectA lightweight event object that acts like a multicast delegate.
Listeners can be added with +=, removed with -=, and all will be called when the event is triggered like a function: event(args…).
Supports both synchronous and asynchronous listeners.