A publish/subscribe and rpc library in python
Project description
pyspoke
A python library supporting pubsub and remote procedure calls on Linux.
Overview
This package provides a python library for passing JSON messages between processes. Clients publish messages to named channels and subscribe to channels to receive those messages. Each client connects to a single server that acts as the message broker. Features include:
- Hierarchical channel names with support for wildcard subscriptions
- Remote procedure calls built on top of the publish/subscribe capability
- Persistent messages that are recorded by the server and sent to clients that later subscribe to the channel
- Bridging servers so that they and their clients behave like a single network
- Customizable transport protocol - default is TCP sockets
- Asynchronous client/server code using standard asyncio module
- Synchronous wrapper functions for publish and rpc calls
Installation
Requires Python 3.7 or greater
From PyPI
Install using pip:
python3 -m pip install pyspoke
From latest source
First install build dependencies:
python3 -m pip install build
Building the distribution:
git clone https://gitlab.com/samflam/pyspoke.git
cd pyspoke
make
To install, you can pip install
the built wheel in dist
or simply run
make install
Testing
From the top level, do:
make test
Examples
Basic publisher and subscriber
First we run the server that acts as a message broker, receiving published messages and sending them to subscribed clients:
"Server code"
import asyncio
import spoke
server = spoke.pubsub.server.Server()
try:
asyncio.run(server.run())
except KeyboardInterrupt:
pass
Next we need a subscriber that will listen for messages on the given channel(s) and execute a callback function when a message is received. In this case we listen for messages on the foo
channel and just print them out:
"Subscriber"
import asyncio
import spoke
async def handle_foo(msg):
print(f"Got message on foo channel: {msg.body}")
async def main():
client = spoke.pubsub.client.Client()
await client.run()
await client.subscribe("foo", handle_foo)
await spoke.wait()
asyncio.run(main())
Finally we publish a message on the foo
channel. This example uses the synchonous wrapper function, which is simpler to use, but must establish a new connection each time it is called:
"Publisher (using simple synchronous call)"
import spoke
spoke.publish("foo", 5)
Connection options
By default, clients assume the server is at localhost:7181
; the server binds to 0.0.0.0:7181
.
The defaults may be changed:
# server and client-subscriber are configured inside a map, the named argument conn_opts:
server = spoke.pubsub.server.Server(conn_opts = {"host": "localhost", "port": 4444, "reuse": True})
server = spoke.pubsub.server.Server(conn_opts = {"reuse": True})
client = spoke.pubsub.client.Client(conn_opts = {"host": "spoke", "port": 8888})
client = spoke.pubsub.client.Client(conn_opts = {"host": "spoke"})
# the publisher is different; it takes host and port themselves as named arguments:
spoke.publish('world', 'Hello!', host="spoke", port=8888)
spoke.publish('dazai', 'BSD', port=8888)
Command line interface
This package provides serveral command line scripts for common tasks. For help on any of them, run with the flag -h
:
spoke-server
- runs a server that acts as the message brokerspoke-echo
- subscribes to the given channels and prints any messages that it receivesspoke-publish
- publish a message on the given channelspoke-call
- do a remote procedure call on the given channel and print the resultspoke-bridge
- connect two spoke servers so that they and their clients transparently behave like a single network
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
File details
Details for the file pyspoke-1.1.0.tar.gz
.
File metadata
- Download URL: pyspoke-1.1.0.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.6 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/21.8.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f625174846873e5611b81dd53a3013c6c01ab87f975d48a2910f053beb78d9e |
|
MD5 | 0a63dfdb11f3503b816e76bd5dbc3d9c |
|
BLAKE2b-256 | 8cbd18d366d0e715af887f560873a4cfcf607f73bb85bfd7fbcf3daadd0b6b3d |
File details
Details for the file pyspoke-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: pyspoke-1.1.0-py3-none-any.whl
- Upload date:
- Size: 15.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.6 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/21.8.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a005b0511b1f00f563e9831725c10ce89d2c689c19a963c0136fe53472d734c |
|
MD5 | 06ea84a2365bc53d2e56811a19705f70 |
|
BLAKE2b-256 | 9e9d2cc049e899d973fe8269c6f83e59c4ed96b9fa3ed3f2ac04e6f90471e24a |