Skip to main content

Peach's Lightweight Event Bus

Project description

PL-EBS

Peach's Lightweight Event Bus Singleton

Low code, low dependency, simple implementation of an Event Bus

Events

Events are the messages that will be published to topics.

Events included in a events/ package at the root of the project are automatically imported to PL-EBS at start up. It is not a requirement that all Event definitions exist in one package but it is recommended as it makes the code cleaner and easier to manage. It is slighly more efficient as well since the penalty of dynamic class creation and registration are paid at initialization and not during run time.

from event_bus import event_message

# Event that uses the class name as the topic
@event_message
class MyEvent:
    data: str

# Event that uses a specific topic name
@event_message(topic="custom.topic")
class AnotherEvent:
    pass

Publish

Once an event is registered it can be used on the event bus. Events are published by calling the publish(topic: str, **kwargs) method. The key word arguments are used to populate the fields defined in the Event's class.

from event_bus import EventBus

EventBus().publish("MyEvent", data="Important Message")

Handlers

Unlike Events, handlers are not self-registered at initialization. Handlers can be registered by using the @event_handler decorator along with the register_handlers(obj: Any) method or calling EventBus().subscribe(topic: str, handler: Callable[BaseEvent])

Using decorators:

from event_bus import event_handler, register_handlers

class MyClass:

    def __init__(self):
        register_handlers(self)

    @event_handler("MyEvent")
    def handler(self, msg):
        do_something(msg)

Subscribing to a topic directly:

from event_bus import EventBus

def handler(msg):
    do_something(msg)

EventBus().subscribe(topic="MyEvent", handler=handler)

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

pl_eb-0.1.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pl_eb-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file pl_eb-0.1.0.tar.gz.

File metadata

  • Download URL: pl_eb-0.1.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for pl_eb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a5ec5858193c470dc8d6f3f344bc39e706d8dba50df3a3eedfefcbc9ef9304ec
MD5 8d2efdff887d8bfa42f8dc36162efdb3
BLAKE2b-256 ef846f17d8a506a53ec1f272f4a2fb3f41f76a28d4453333557f099242ebd799

See more details on using hashes here.

File details

Details for the file pl_eb-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pl_eb-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for pl_eb-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 15bbf49b1a17eb550fc457986985ba6461bbf63ea762022cec3d49e74d025e54
MD5 327b6a1e496d705d79f3671aa770dbdd
BLAKE2b-256 ab27cdbf64d655d24f77dba25183fe84bf9fed90d387e02484a6a2c4cd3a5655

See more details on using hashes here.

Supported by

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