Tiny pub-sub (observer) pattern implementation
Project description
tinypubsub
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.Publisherhas abstract methods:publish,subscribe,unsubscribe,unsubscribe_all.tinypubsub.simple.SimplePublisher: Dict-based implementation ofPublisher.tinypubsub.weakref.WeakrefPublisher: WeakKeyDictionary-based implementation ofPublisher.
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tinypubsub-0.1.0.tar.gz.
File metadata
- Download URL: tinypubsub-0.1.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.1 Darwin/19.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
16a3934d99835d74f97035125d386a38e7aa2b24efa68edd8fa2474906587189
|
|
| MD5 |
cf5154400debde0033588e69b8671db8
|
|
| BLAKE2b-256 |
10006ca505b959ea3d0aad7c2a5c351d093834b70bff441568342f042e33a95b
|
File details
Details for the file tinypubsub-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tinypubsub-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.1 Darwin/19.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
84e48954d4d41765e0aa245b736ec529a6cdd79f21cb285c809a94869699f8ef
|
|
| MD5 |
2c532d8d90cc95b9bda60639194f7c31
|
|
| BLAKE2b-256 |
e40bbeb825eed1e77143b3f72ce7c9f984e9f27496c3c97fcc33d02a47257af0
|