Skip to main content

a Python NATS client

Project description

a Python NATS client

Why?

I like NATS a lot.

asyncio less so hence never played with the official Python client.

While I got a lot out of nats-python, it has some rought edges I would rather not have to work around as an end user.

I wanted to teach myself enough socket programming and gain a deeper understanding of the NATS protocol.

a Python NATS client that fits the way I use NATS.

  • Client must have a name
  • Client PONGs on PING behind the scene
  • Encourage to only use Client as a contextmanager
  • non blocking when receiving messages from subscriptions
  • blocking on request / response

It is far from feature complete.

It implements the features I use, which maybe all you need too.

How?

A contrived example illustrating how an application would interact with a publisher and a responder via NATS.

Fire up NATS on your dev box and python -m goingnats to see it run.

import datetime as dt
import time
import threading

from goingnats import Client


def publisher():
    """publish time.time() every second"""
    with Client(name="publisher") as client:
        while True:
            time.sleep(1)
            client.publish(subject="time.time", payload=f"{time.time()}")

threading.Thread(target=publisher, daemon=True).start()

def responder():
    """respond to request for today with the date"""
    with Client(name="responder") as client:
        client.subscribe(subject="today")
        while True:
            for request in client.get():
                if request.subject != "today":
                    continue
                # slow responder
                time.sleep(2)
                # will format the date according to payload or defaults to ...
                format = (
                    request.payload.decode("utf-8")
                    if request.payload
                    else "%Y-%m-%d"
                )
                client.publish(
                    subject=request.inbox,
                    payload=f"{dt.date.today():{format}}",
                )

threading.Thread(target=responder, daemon=True).start()

# application
with Client(name="consumer") as client:
    client.subscribe(subject="time.time")
    received = 0
    response = None
    while received < 5:
        for message in client.get():
            print(message)
            received += 1
        if received == 3 and response is None:
            # request response are blocking
            response = client.request(subject="today", payload="%Y%m%d")
            print(response)

one more thing

>>> from goingnats import one
>>> one(subject=">")
Message(...)

one is a very handy little helper that waits to receive a message on a given subject and returns it.

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

goingnats-2021.3.6.tar.gz (4.2 kB view details)

Uploaded Source

File details

Details for the file goingnats-2021.3.6.tar.gz.

File metadata

  • Download URL: goingnats-2021.3.6.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.1.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.1

File hashes

Hashes for goingnats-2021.3.6.tar.gz
Algorithm Hash digest
SHA256 831013357be62694e3bc02a99b2f409116317db37e7e06d65c32c30b28d1bf30
MD5 2adde0265158696312b34fb14930b9c4
BLAKE2b-256 aae605a40611b5cbb439c5685d198551924eff7128fbcb28624e2659191551fa

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