Skip to main content

A lightweight third-party broadcast/pubsub library

Project description

broadcast-service

A lightweight python broadcast library. You can easily construct a Broadcast pattern/Publish subscriber pattern through this library.

github stars

Features

  • A publishing subscriber pattern can be built with a very simple syntax
  • Support different application scenarios, such as asynchronous and synchronous
  • Provide different syntax writing modes for lambda, callback functions, decorators, etc
  • A callback function listens on multiple subscriptions
  • Provide publisher dispatch callback manage

Quick Start

Setup

pip install broadcast-service

Usage

There is an easy demo to show how to use broadcast-service.

from broadcast_service import broadcast_service

# callback of decorator
@broadcast_service.on_listen('my_topic')
def handle_decorator_msg(params):
    print(f"handle_decorator_msg receive params: {params}")

if __name__ == '__main__':
    info = 'This is very important msg'

    # publish broadcast
    broadcast_service.publish('my_topic', info)
  • You can use publish, emit, broadcast to send your topic msg and use listen, on, subscribe to listen your topic msg.

  • You can also add more arguments or no argument when you publish thr broadcast.

from broadcast_service import broadcast_service

# subscribe topic
@broadcast_service.on_listen(['my_topic'])
def handle_msg(info, info2):
    print(info)
    print(info2)

if __name__ == '__main__':
    info = 'This is very important msg'
    info2 = 'This is also a very important msg.'

    # publish broadcast
    broadcast_service.publish('my_topic', info, info2)
from broadcast_service import broadcast_service

# subscribe topic
@broadcast_service.on_listen(['my_topic'])
def handle_msg():
    print('handle_msg callback')

if __name__ == '__main__':
    # publish broadcast
    broadcast_service.publish('Test')

You can use config to make publisher callback If you want.

from broadcast_service import broadcast_service


@broadcast_service.on_listen("topic")
def handle_subscriber_callback():
    print("handle_subscriber_callback")


def handle_publisher_callback(*args):
    print("handle_publisher_callback")


if __name__ == '__main__':
    broadcast_service.config(
        num_of_executions=5,
        callback=handle_publisher_callback,
        enable_final_return=True,
        interval=0.1
    ).publish("topic")

Moreover, you can see more example in document.

TODO

  • optimize documents and show more examples.
  • optimize the syntax expression of broadcast-service
  • provide more test cases
  • provide the ability to subscribe the topic and callback once
  • support for fuzzy subscriptions
  • the publisher of the topic can provide a return value
  • optimize usage in class ('self' params problem)
  • build observer mode
  • provide publisher callback when all subscriber have completed callback

Contribution

If you want to contribute to this project, you can submit pr or issue. I am glad to see more people involved and optimize it.

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

broadcast_service-2.0.0.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

broadcast_service-2.0.0-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file broadcast_service-2.0.0.tar.gz.

File metadata

  • Download URL: broadcast_service-2.0.0.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for broadcast_service-2.0.0.tar.gz
Algorithm Hash digest
SHA256 8111d0cff99aa15a20a8dbf5783ce07b1581b29e94900a2f504979b09d2b0c59
MD5 850d3f5ffe956a1cc7267ae27c4e25eb
BLAKE2b-256 61ff746a9078193ec9f27ea383c4ff6169303536b57a0708681d106c2ee554d9

See more details on using hashes here.

File details

Details for the file broadcast_service-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for broadcast_service-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 47cc9ca48254e84bf6c9e898ff0a59f666e207d30e6adf958bc5d7f91023d63a
MD5 f0314176d5eaf3d6f9f6b5a77092be68
BLAKE2b-256 23075b4a63706f94a499567e71063fb5cfe11dca0a69bf7ecda707831aa78ffa

See more details on using hashes here.

Supported by

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