Skip to main content

tinypubsub

PyPI PyPI - Python Version Code style: black license

Tiny pub-sub (observer) pattern implementation.

Usage

from tinypubsub.simple import SimplePublisher

publisher = SimplePublisher()

subscription = publisher.subscribe(lambda message: print(message))

publisher.publish("Hello!")

publisher.unsubscribe(subscription)

Or:

from tinypubsub.simple import SimplePublisher

publisher = SimplePublisher()

with publisher.subscribe(lambda message: print(message)):
    publisher.publish("Hello!")

Features

  • tinypubsub.Publisher has abstract methods: publish, subscribe, unsubscribe, unsubscribe_all.
  • tinypubsub.simple.SimplePublisher: Dict-based implementation of Publisher.
  • tinypubsub.weakref.WeakrefPublisher: WeakKeyDictionary-based implementation of Publisher.

API

Module tinypubsub

abstract class Publisher[Message]

Abstract publisher class.

type parameter Message

Type of message that will be published.

abstract method publish(message: Message) -> None

Publish message to subscribers.

abstract method subscribe(subscriber: Callable[[Message], None]) -> Subscription

Add subscriber.

abstract method unsubscribe(subscription: Subscription) -> None

Delete subscriber.

abstract method unsubscribe_all() -> None

Delete all subscribers.

class Subscription

Return value of Publisher.subscribe(). It can be used as a context manager as:

with publisher.subscribe(...) as subscription:
    ...

and subscription.unsubscribe() will be called when exit.

method unsubscribe() -> None

subscription.unsubscribe() is the same as publisher.unsubscribe(subscription), where subscription = publisher.subscribe(...).

Module tinypubsub.simple

class SimplePublisher[Message]

Implementation of Publisher[Message].

Module tinypubsub.weakref

class WeakrefPublisher[Message]

Implementation of Publisher[Message].

This implementation uses WeakKeyDictionary to manage subscribers. This may prevent a memory leak if subscription loses all strong references before unsubscribed:

publisher = WeakrefPublisher()

subscription = publisher.subscribe(...)

assert len(publisher._subscribers) == 1

del subscription

assert len(publisher._subscribers) == 0

Note that the unsubscribe method will not be called in the above case, so normally you should unsubscribe explicitly or use context manager.

Metadata

Release files for tinypubsub 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tinypubsub 0.1.0
File Size Uploaded Provenance Actions
tinypubsub-0.1.0.tar.gz 4.2 kB Without provenance Details for tinypubsub-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tinypubsub 0.1.0
File Interpreter ABI Platform Actions
tinypubsub-0.1.0-py3-none-any.whl Python 3 none any Details for tinypubsub-0.1.0-py3-none-any.whl

Total release size: 9.7 kB

Release files / tinypubsub-0.1.0.tar.gz

Download URL tinypubsub-0.1.0.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
16a3934d99835d74f97035125d386a38e7aa2b24efa68edd8fa2474906587189
BLAKE2b-256 checksum
How to use checksums
10006ca505b959ea3d0aad7c2a5c351d093834b70bff441568342f042e33a95b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.9.1 Darwin/19.6.0

Release files / tinypubsub-0.1.0-py3-none-any.whl

Download URL tinypubsub-0.1.0-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
84e48954d4d41765e0aa245b736ec529a6cdd79f21cb285c809a94869699f8ef
BLAKE2b-256 checksum
How to use checksums
e40bbeb825eed1e77143b3f72ce7c9f984e9f27496c3c97fcc33d02a47257af0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.9.1 Darwin/19.6.0

Security for tinypubsub 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page