Skip to main content

Core NATS Extensions is a set of utilities providing additional features to Core NATS component of nats-py client.

Project description

Core NATS Extensions

Core NATS Extensions is a set of utilities providing additional features to Core NATS component of nats-py client.

Installation

uv add natsext

Utilities

request_many

request_many is a utility that allows you to send a single request and await multiple responses. This allows you to implement various patterns like scatter-gather or streaming responses.

Responses are returned in an async iterator, which you can iterate over to receive messages. When a termination condition is met, the iterator is closed (and no error is returned).

import natsext

# Basic usage
async for msg in natsext.request_many(nc, "subject", b"request data"):
    print(msg.data)

Alternatively, use request_many_msg to send a nats.Msg request:

import nats
import natsext

msg = nats.Msg(
    _client=nc,
    subject="subject",
    data=b"request data",
    headers={
        "Key": "Value",
    },
)
async for response in natsext.request_many_msg(nc, msg):
    print(response.data)

Configuration

You can configure the following options:

  • timeout: Overall timeout for the request operation (float, seconds)
  • stall: Stall timer, useful in scatter-gather scenarios where subsequent responses are expected within a certain timeframe (float, seconds)
  • max_messages: Maximum number of messages to receive (int)
  • sentinel: Function that stops returning responses once it returns True for a message (Callable[[Msg], bool])
# With all options
async for msg in natsext.request_many(
    nc,
    "subject",
    b"request data",
    timeout=5.0,
    stall=0.1,
    max_messages=10,
    sentinel=natsext.default_sentinel  # Stops on empty message
):
    print(msg.data)

Default Sentinel

The package includes a default_sentinel function that stops receiving messages once a message with an empty payload is received:

import natsext

async for msg in natsext.request_many(
    nc, "subject", b"request", sentinel=natsext.default_sentinel
):
    print(msg.data)

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

natsext-0.1.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

natsext-0.1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file natsext-0.1.0.tar.gz.

File metadata

  • Download URL: natsext-0.1.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.15

File hashes

Hashes for natsext-0.1.0.tar.gz
Algorithm Hash digest
SHA256 96d1519af933caef3bee7198abfc5ef5dd11307aa59f8bdb0fbd29a3953cb6ce
MD5 084fda20aed4997a11a1364634ed70aa
BLAKE2b-256 dc62e062f172ad7cb3cd14efa9f6fd150a71fc1edcb8e997e82b03e591ac92f2

See more details on using hashes here.

File details

Details for the file natsext-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: natsext-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.15

File hashes

Hashes for natsext-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 da797da0b31945e145d458548df2bce2058ad6f34dfe604d77f13fd8634b9270
MD5 c41035e46e7f82aad611b290b2841e96
BLAKE2b-256 05a2e6f3911549740f2749b33d1cbc32539b5f978ae91dfdfbcf6d7b9aa8804d

See more details on using hashes here.

Supported by

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