Skip to main content

Minimalistic, thread-safe, publish-subscribe API for Python

Welcome to subpub

PyPI-Versions PyPI-Wheel PyPI-Downloads Read-the-Docs License

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.

Metadata

Release files for subpub 1.0.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 subpub 1.0.0
File Size Uploaded Provenance Actions
subpub-1.0.0.tar.gz 6.5 kB Without provenance Details for subpub-1.0.0.tar.gz

Release files / subpub-1.0.0.tar.gz

Download URL subpub-1.0.0.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
51cbb088e3d800b9684651e39da87e7586adb2a38aed9d61dc81182de01fedde
BLAKE2b-256 checksum
How to use checksums
8727bbd56cedc4685f789e207b1a75397551071ae0578d31eedf5ac864ea25cf
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for subpub 1.0.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

1.0.0 This release

1 release file 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