Skip to main content

linkr

Async messaging framework with three delivery modes: RPC, task and event.

Install

pip install linkr

Delivery modes

Mode Registration Client Behaviour
RPC @app.method("foo") await app.call("orders.foo", (), {}) One consumer handles the call and returns a result.
Task @app.task("email.send") await app.send("worker.email.send", (), {"user_id": 1}) One of the consumers handles the task. No result.
Event @app.subscribe("user.created") await app.emit("user.created", {"user_id": 1}) Every subscriber receives the event. No result.

The client methods take positional arguments as a tuple and keyword arguments as a dict:

await app.call("orders.add", (2, 3), {})
await app.call("orders.add", (), {"x": 2, "y": 3})
await app.send("worker.email.send", (), {"user_id": 1})
await app.emit("user.created", {"user_id": 1})

RPC

By default all RPC methods of an app share one queue. Dispatch happens by method name.

from linkr import App, LocalBackend

backend = LocalBackend()

async with App("orders", backend=backend) as app:
    @app.method("foo")
    def foo() -> str:
        return "foo"

    @app.method("bar")
    def bar() -> str:
        return "bar"

    await app.consume()

    print(await app.call("orders.foo", (), {}))  # foo
    print(await app.call("orders.bar", (), {}))  # bar

Both foo and bar are consumed from the orders queue.

A method name may contain a group prefix. A grouped method gets its own queue:

@app.method("group1.foo")
def foo() -> str:
    return "foo"

@app.method("group2.bar")
def bar() -> str:
    return "bar"

For the app named orders:

Method Queue
foo orders
group1.foo orders.group1
group2.bar orders.group2

Calls use the fully qualified queue path:

await app.call("orders.group1.foo", (), {})
await app.call("orders.group2.bar", (), {})

To call another app, use its queue prefix:

await client.call("orders.foo", (), {})
await client.call("orders.group1.foo", (), {})

Dotted method names are queue-qualified paths. A plain name targets the calling application's default queue.

Task

A task has no response. Multiple consumers may listen on the same task queue; each task is handled by exactly one of them.

async with App("worker", backend=backend) as app:
    @app.task("email.send")
    async def send_email(user_id: int) -> None:
        ...

    await app.consume()

    # From the same app:
    await app.send("worker.email.send", (), {"user_id": 1})

    # From another app:
    await client.send("worker.email.send", (), {"user_id": 1})

Queue naming:

Task method Queue
send worker
email.send worker.email

Task names passed to send() use the same queue-qualified convention as RPC calls.

Event

An event is delivered to every subscriber. Each app instance gets its own event queue, so multiple instances all receive the event.

Event queue naming:

App name Instance ID Queue
billing abc123 billing.abc123
billing def456 billing.def456
async with App("billing", backend=backend) as app:
    @app.subscribe("user.created")
    def on_user_created(user_id: int) -> None:
        ...

    await app.consume()

    await app.emit("user.created", {"user_id": 1})

The event payload is passed to subscribers as keyword arguments.

Wildcards are supported:

  • * matches exactly one segment: user.* matches user.created.
  • # matches zero or more segments: user.# matches user.created and user.profile.updated.

Multiple subscribers for the same topic in one app are all called.

Lifecycle

await app.consume()                 # start RPC, task and event consumers
await app.consume_rpc()             # start RPC consumers only
await app.consume_tasks()           # start task consumers only
await app.consume_events()          # start event consumers only

await app.stop_consume()            # stop all consumers
await app.stop_consume_rpc()        # stop all RPC consumers
await app.stop_consume_rpc("foo")   # stop a specific RPC method (queue stops when unused)
await app.stop_consume_tasks("email.send")
await app.stop_consume_events("user.created")
await app.close()

Stopping a specific RPC method or task removes that handler from the app. The backend queue consumer is stopped only when no other handlers of the same mode share that queue. Stopping a specific event topic removes that subscription; if other topic subscriptions remain, only that topic is removed. Backends expose stop_consume_rpc(queue=None), stop_consume_tasks(queue=None) and stop_consume_events(queue=None, topic=None) as execution commands; App decides when a queue/topic should be stopped.

RPC/task router options

Use app.rpc_router(...) / app.task_router(...) to configure backend-specific options for a named RPC/task queue and (optionally) register handlers on it. Routers do not apply to event queues and do not configure the default app queue (app.name) — default and event queue options are static backend configuration.

from linkr import App, LocalBackend
from linkr.backends.rmq import RmqQueueOptions  # backend-specific

app = App("orders", backend=LocalBackend())

# named RPC queue (orders.serviceA)
rpc = app.rpc_router("serviceA", options=[RmqQueueOptions(prefetch_count=5, durable=True)])

@rpc("ping")
def ping() -> str:
    return "pong"

# named task queue (orders.email), shared by handlers
email = app.task_router("email", options=[RmqQueueOptions(prefetch_count=50)])

@email("send")
async def send(user_id: int) -> None:
    ...

await app.consume()
  • path is required and must not be empty; it targets the named queue app.<path>.
  • @router("name") registers a leaf handler on that queue (<path>.<name>). The handler name must not contain ..
  • Options are opaque to App: pass a QueueOptions instance or a list and the active backend selects the types it understands.
  • Configure before consume_*. One queue has one option set (identical re-declaration is idempotent, conflicting raises; cannot reconfigure while consuming). No declaration keeps backend defaults. LocalBackend ignores transport options.

Middleware

App-level middleware

AppMiddleware works with deserialized Request/Response objects. Request.type tells which delivery mode is being processed.

import logging
from collections.abc import Awaitable, Callable
from typing import Any

from linkr import App, AppMiddleware, LocalBackend
from linkr.models import Request, Response


class LoggingMiddleware(AppMiddleware):
    async def dispatch_client(
        self,
        call_next: Callable[[], Awaitable[Response | None]],
        request: Request,
        *,
        context: dict[str, Any],
    ) -> Response | None:
        logging.info("[%s] Calling %s", request.id, request.data.get("method") or request.data.get("topic"))
        response = await call_next()
        if response:
            logging.info("[%s] Done", request.id)
        return response

    async def dispatch_server(
        self,
        call_next: Callable[[], Awaitable[Response | None]],
        request: Request,
    ) -> Response | None:
        logging.info("[%s] Calling %s", request.id, request.data.get("method") or request.data.get("topic"))
        return await call_next()

For task and event messages the response path is skipped.

Wire-level middleware

Compression and encryption operate on raw bytes and wire headers.

from linkr.middleware.gzip import GzipMiddleware

app.add_middleware(GzipMiddleware())

Custom wire middleware inherits from WireMiddleware:

import gzip
from collections.abc import Awaitable, Callable
from typing import Any

from linkr import WireMiddleware
from linkr.models import Message, Request, Response


class CustomCompression(WireMiddleware):
    async def dispatch_client(
        self,
        call_next: Callable[[], Awaitable[Message | None]],
        request: Request,
        request_message: Message,
        *,
        context: dict[str, Any],
    ) -> Message | None:
        if len(request_message.data) >= 1024:
            request_message.data = gzip.compress(request_message.data)
            request_message.headers["content_encoding"] = "gzip"
        response_message = await call_next()
        if response_message and "gzip" in response_message.headers.get("content_encoding", ""):
            response_message.data = gzip.decompress(response_message.data)
        return response_message

    async def dispatch_server(
        self,
        call_next: Callable[[], Awaitable[tuple[Message, Response] | tuple[None, None]]],
        request_message: Message,
    ) -> tuple[Message, Response] | tuple[None, None]:
        if "gzip" in request_message.headers.get("content_encoding", ""):
            request_message.data = gzip.decompress(request_message.data)
        result = await call_next()
        if result is None or result[0] is None:
            return None, None
        response_message, response = result
        if len(response_message.data) >= 1024:
            response_message.data = gzip.compress(response_message.data)
            response_message.headers["content_encoding"] = "gzip"
        return response_message, response

Dependency Injection

from linkr import App, Depends, LocalBackend, get_current_request


class Database:
    def __init__(self, url: str) -> None:
        self.url = url


backend = LocalBackend()

async with App("orders", backend=backend) as app:
    @app.method("ping")
    def ping(db: Depends[Database, lambda: Database("postgres://...")]) -> str:
        req = get_current_request()
        return f"{req.type}:{db.url}"

    await app.consume()
    print(await app.call("orders.ping", (), {}))

Depends[Type, factory] resolves a dependency by calling factory() for each handler execution. get_current_request() returns the request being processed in the current async context for RPC, task and event handlers.

Error Handling

Type validation is enabled per handler with validate_types=True or globally with App(..., validate_types=True):

from linkr import App, LocalBackend, RPCError

backend = LocalBackend()

async with App("orders", backend=backend) as app:
    @app.method("add", validate_types=True)
    def add(x: int, y: int) -> int:
        return x + y

    @app.task("process", validate_types=True)
    async def process(data: dict[str, int]) -> None:
        ...

    @app.subscribe("user.created", validate_types=True)
    def on_user_created(user_id: int, name: str) -> None:
        ...

    await app.consume()
    try:
        await app.call("orders.add", (), {"x": "not", "y": 3})
    except RPCError as e:
        print(e.error_code)     # ValidationError
        print(e.error_message)

    # Task validation failure logs warning, handler not called
    await app.send("orders.process", (), {"data": {"x": "bad"}})

    # Event validation failure logs warning, handler not called
    await app.emit("user.created", {"user_id": "bad", "name": "John"})

Custom exception handlers:

@app.exception_handler(ValueError)
def handle_value_error(exc: Exception) -> tuple[str, str, dict[str, str]]:
    return ("InvalidValueError", str(exc), {"field": "x"})

Task and event handlers cannot return an error response, so their exceptions are logged.

Serializers

The default is JsonSerializer (linkr1.0). JSON-RPC 2.0 is available via JsonRpcSerializer and supports RPC requests only:

from linkr import App, JsonRpcSerializer, JsonSerializer, LocalBackend

app = App("orders", backend=LocalBackend(), serializer=JsonRpcSerializer())

A list of serializers enables auto-detection on the server; the first entry is the default for outgoing calls:

app = App(
    "orders",
    backend=LocalBackend(),
    serializer=[JsonRpcSerializer(), JsonSerializer()],
)

Tasks and events should use JsonSerializer (or a multi-serializer configuration where the outgoing call explicitly selects it).

Backends

Backend Status
LocalBackend In-process, for tests and local development
RmqBackend Implemented; requires RabbitMQ

License

MIT

Metadata

Release files for linkr 0.5.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 linkr 0.5.0
File Size Uploaded Provenance Actions
linkr-0.5.0.tar.gz 45.1 kB Without provenance Details for linkr-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for linkr 0.5.0
File Interpreter ABI Platform Actions
linkr-0.5.0-py3-none-any.whl Python 3 none any Details for linkr-0.5.0-py3-none-any.whl

Total release size: 93.3 kB

Release files / linkr-0.5.0.tar.gz

Download URL linkr-0.5.0.tar.gz
Size 45.1 kB
Tags Source
SHA-256 checksum
How to use checksums
5168c476b1c2283c0e9f4fb7b6c1c613c37025523b100ca4d460bc222cddd088
BLAKE2b-256 checksum
How to use checksums
7c76b95311f508a24cae784048d88d3b325b103db38be62dcba88a6f7452f4ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.2 CPython/3.14.3 Darwin/27.2.0

Release files / linkr-0.5.0-py3-none-any.whl

Download URL linkr-0.5.0-py3-none-any.whl
Size 48.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
535eb777e5152d72d0aaef596b8e4f5cb157045e69cb7e01fb98dd61eb37b6b9
BLAKE2b-256 checksum
How to use checksums
e07f635a0f338eaef7086c04b3af332cba9a065ccf68f783db2917fd2e74c769
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.2 CPython/3.14.3 Darwin/27.2.0

Security for linkr 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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