Minimalistic, thread-safe, publish-subscribe API for Python
Project description
Minimalistic, thread-safe, publish-subscribe API for Python
Welcome to subpub
subpub provides a minimalistic, thread-safe, publish-subscribe API for single-process Python applications.
The latest documentation is available on Read the Docs.
The source code is available on GitHub.
Example
The example below demonstrates basic usage.
# Create an instance of the message broker
>>> from subpub import SubPub
>>> sp = SubPub()
# Subscribe to a topic (= any string or regular expression).
# The returned queue `q` is used to retrieve published data:
>>> q = sp.subscribe(r'/food/(\w+)/order-(\d+)')
# Publish any data to topic:
>>> sp.publish('/food/pizza/order-66', "beef pepperoni")
True
# Get the published data from the queue:
>>> match, data = q.get()
>>> data
'beef pepperoni'
# The queue always receives the regexp `match` object as well.
# It can be used to see how the topic matched and get groups:
>>> match.groups()
('pizza', '66')
# Get the published topic:
>>> match.string
'/food/pizza/order-66'
See test cases in test_subpub.py for more examples.
Key features
SubPub’s methods subscribe, unsubscribe, unsubscribe_all and publish are thread-safe.
Subscribers use regular experssions to filter on topic.
Subscribers receive published data through queues. (There is no built-in mechanism to register callbacks.)
When an queue is garbage collected, unsubscribe is executed automatically (because SubPub only keeps a weak reference to the subscribers’ queues).
Publishers can post any Python object as message.
Publishers can use retain=True to store a message (as in MQTT).
Installation
From PyPI:
$ python3 -m pip install subpub
Reference
See module reference at Read the Docs.
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
File details
Details for the file subpub-1.0.0.tar.gz
.
File metadata
- Download URL: subpub-1.0.0.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51cbb088e3d800b9684651e39da87e7586adb2a38aed9d61dc81182de01fedde |
|
MD5 | 848aa8b17f74e5de725e35a439406148 |
|
BLAKE2b-256 | 8727bbd56cedc4685f789e207b1a75397551071ae0578d31eedf5ac864ea25cf |