Skip to main content

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)

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 broker
  • spoke-echo - subscribes to the given channels and prints any messages that it receives
  • spoke-publish - publish a message on the given channel
  • spoke-call - do a remote procedure call on the given channel and print the result
  • spoke-bridge - connect two spoke servers so that they and their clients transparently behave like a single network

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyspoke-1.0.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

pyspoke-1.0.0-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file pyspoke-1.0.0.tar.gz.

File metadata

  • Download URL: pyspoke-1.0.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.4

File hashes

Hashes for pyspoke-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9cec5624f854a9c271af9a5dfc79b86b10e96a699bcc48acc5194d10ceb37710
MD5 79aa1a04786f2b66e3000523b2764d07
BLAKE2b-256 c52a7aec074142ac0985e27205ca9954ae946e4460fa031ddc5f83575a8a7074

See more details on using hashes here.

File details

Details for the file pyspoke-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyspoke-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.4

File hashes

Hashes for pyspoke-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aa8a200d47b304cf89d2313f289215a4a99db96f74dc267ff0475716db16549b
MD5 6a4eec2d8fce971ff9f7c06a656c7906
BLAKE2b-256 9b41894311cf79500854cac0e7d0ecdb1a9908e2a4bb7c582736ec7004689d21

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page