Skip to main content

Taskiq NATS

PyPI - Python Version PyPI PyPI - Downloads

Taskiq-nats is a plugin for taskiq that adds NATS broker. This package has support for NATS JetStream.

Installation

To use this project you must have installed core taskiq library:

pip install taskiq taskiq-nats

Usage

Here's a minimal setup example with a broker and one task.

Default NATS broker.

import asyncio
from taskiq_nats import NatsBroker, JetStreamBroker

broker = NatsBroker(
    [
        "nats://nats1:4222",
        "nats://nats2:4222",
    ],
    queue="random_queue_name",
)


@broker.task
async def my_lovely_task():
    print("I love taskiq")


async def main():
    await broker.startup()

    await my_lovely_task.kiq()

    await broker.shutdown()


if __name__ == "__main__":
    asyncio.run(main())

NATS broker based on JetStream

import asyncio
from taskiq_nats import (
    PushBasedJetStreamBroker,
    PullBasedJetStreamBroker
)

broker = PushBasedJetStreamBroker(
    servers=[
        "nats://nats1:4222",
        "nats://nats2:4222",
    ],
    queue="awesome_queue_name",
)

# Or you can use pull based variant
broker = PullBasedJetStreamBroker(
    servers=[
        "nats://nats1:4222",
        "nats://nats2:4222",
    ],
    durable="awesome_durable_consumer_name",
)


@broker.task
async def my_lovely_task():
    print("I love taskiq")


async def main():
    await broker.startup()

    await my_lovely_task.kiq()

    await broker.shutdown()


if __name__ == "__main__":
    asyncio.run(main())

NatsBroker configuration

Here's the constructor parameters:

  • servers - a single string or a list of strings with nats nodes addresses.
  • subject - name of the subect that will be used to exchange tasks between workers and clients.
  • queue - optional name of the queue. By default NatsBroker broadcasts task to all workers, but if you want to handle every task only once, you need to supply this argument.
  • result_backend - custom result backend.
  • task_id_generator - custom function to generate task ids.
  • Every other keyword argument will be sent to nats.connect function.

JetStreamBroker configuration

Common

  • servers - a single string or a list of strings with nats nodes addresses.
  • subject - name of the subect that will be used to exchange tasks between workers and clients.
  • stream_name - name of the stream where subjects will be located.
  • queue - a single string or a list of strings with nats nodes addresses.
  • result_backend - custom result backend.
  • task_id_generator - custom function to generate task ids.
  • stream_config - a config for stream.
  • consumer_config - a config for consumer.

PushBasedJetStreamBroker

  • queue - name of the queue. It's used to share messages between different consumers.

PullBasedJetStreamBroker

  • durable - durable name of the consumer. It's used to share messages between different consumers.
  • pull_consume_batch - maximum number of message that can be fetched each time.
  • pull_consume_timeout - timeout for messages fetch. If there is no messages, we start fetching messages again.

NATS Result Backend

It's possible to use NATS JetStream to store tasks result.

import asyncio
from taskiq_nats import PullBasedJetStreamBroker
from taskiq_nats.result_backend import NATSObjectStoreResultBackend


result_backend = NATSObjectStoreResultBackend(
    servers="localhost",
)
broker = PullBasedJetStreamBroker(
    servers="localhost",
).with_result_backend(
    result_backend=result_backend,
)


@broker.task
async def awesome_task() -> str:
    return "Hello, NATS!"


async def main() -> None:
    await broker.startup()
    task = await awesome_task.kiq()
    res = await task.wait_result()
    print(res)
    await broker.shutdown()


if __name__ == "__main__":
    asyncio.run(main())

Release files for taskiq-nats 0.7.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 taskiq-nats 0.7.0
File Size Uploaded
taskiq_nats-0.7.0.tar.gz 8.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for taskiq-nats 0.7.0
File Interpreter ABI Platform
taskiq_nats-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.4 kB

Release files / taskiq_nats-0.7.0.tar.gz

Download URL taskiq_nats-0.7.0.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
ab0c457fc780fd236db6e004ed445a422670525daff2c983355e1c387db160b9
BLAKE2b-256 checksum
How to use checksums
274e1d4a2a081f1f0eacd30866554913ffe36ea781591c14bc84a22a7323aa95
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / taskiq_nats-0.7.0-py3-none-any.whl

Download URL taskiq_nats-0.7.0-py3-none-any.whl
Size 9.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
667f4c44cd0114dc5ad9a08355a398aaffc009c5b01459aa986348cdf23fb97a
BLAKE2b-256 checksum
How to use checksums
807e8a25645e3598f6ef67eac71d04bdf9a6f6addc63b39729943a12e1c97fab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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