Skip to main content

Event bus implementation for Python

Project description

PyEventBus

EventBus implementations in Python. It allows you to easily publish custom domain events and subscribe commands to them.

Installation

pip install python3-eventbus

Usage

  1. Create the event bus (see "EventBus types" section for more details)
sender_topic = boto3.resource("sns", region_name="us-east-1").Topic(sns_sender_arn)
eventbus = SNSEventBus(sender_topic)
  1. Define your event
@dataclass
class UserCreatedEvent(DomainEvent):
    user_id: str

    def to_dict(self) -> dict[str, str]:
        return {"user_id": self.user_id}

    @classmethod
    def from_dict(cls, data: dict[str, str]) -> Self:
        return cls(data["user_id"])
  1. Define the subscriber
@dataclass
class SendWelcomeEmailCommand:
    eventbus: EventBus

    def __post_init__(self):
        self.eventbus.subscribe(
            UserCreatedEvent, # The event to subscribe to
            lambda event: self.send_welcome_email(event.user_id), # The callback
            self.__class__, # The subscriber entity
        )

    def send_welcome_email(self, user_id: str):
        ...Business logic...
  1. Register the subscriber
send_welcome_email_command = SendWelcomeEmailCommand(eventbus) # This will execute the __post_init__ method (dataclass feature)
  1. Publish the event
eventbus.publish(UserCreatedEvent("user_id"))
  1. From your controller, rebuild the domain event based on the subscriptions
def lambda_handler(event: dict[str, Any], context: Any):
    raw_domain_event = _unwrap_event(event) # Remove event metadata (SQS, SNS, etc.)
    domain_event = eventbus.build_event_from_subscriptions(
        raw_domain_event["event_type"],
        raw_domain_event["event"],
    )

EventBus types

AWS: SNSEventBus

This implementation will forward events to an AWS SNS topic. The topic must be created before the event bus is used.

sender_topic = boto3.resource("sns", region_name="us-east-1").Topic(sns_sender_arn)
eventbus = SNSEventBus(sender_topic)

Complete example: aws_sns.ipynb

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

python3_eventbus-0.1.6.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

python3_eventbus-0.1.6-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file python3_eventbus-0.1.6.tar.gz.

File metadata

  • Download URL: python3_eventbus-0.1.6.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for python3_eventbus-0.1.6.tar.gz
Algorithm Hash digest
SHA256 fde57a5b8aa44b38782f5de602844438054dbd40b36568aeec8d8e3589971491
MD5 c8894d745b113ed463b178026c0278cc
BLAKE2b-256 b21832bc4bc779033c96902e8c9fe078ca029fdfc2b9554531b24d19bbb290a5

See more details on using hashes here.

File details

Details for the file python3_eventbus-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for python3_eventbus-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 26e561ffc16c4b61f51bc50a73a9b860205b4a6845700ced501672b3f7d3c93f
MD5 3466fc4fcf9a1a3e0319fececebca7ad
BLAKE2b-256 d66ec7ccaeb01a4316a9d64aaec2926f6834cf73ca0ea3a897ddaf3c6b95dadb

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