Skip to main content

Python bindings for mq-bridge (full: all brokers incl. Kafka)

Project description

mq-bridge Python bindings

Thin Python bindings for the Rust mq-bridge core.

Install

Pick exactly one distribution. Both install the same import path: mq_bridge.

Package Install Includes
Full pip install mq-bridge-py Basic set plus Kafka, AWS, gRPC, MongoDB, SQLx
Basic pip install mq-bridge-py-basic HTTP, NATS, MQTT, AMQP, WebSocket, ZeroMQ, middleware

Memory and file endpoints are always present in both packages. Use mq-bridge-py-basic when you want the lean all-platform wheel set. Use mq-bridge-py when you need Kafka or the heavier non-messaging backends.

The public API stays close to mq-bridge itself:

  • Route.from_yaml(path, name) loads one named route from a YAML file
  • Route.from_yaml_str(text, name) / Route.from_config(mapping, name) build a route from an in-memory YAML string or a Python dict, no file required
  • Route.with_handler(...) attaches a raw Message handler, with lazy json()/text() readers and with_json()/with_payload() response helpers
  • Route.add_handler(kind, ...) uses mq-bridge's kind dispatch and delivers decoded JSON
  • RetryableError and NonRetryableError let Python handlers signal retry intent
  • Publisher.from_yaml(path, name) (plus from_yaml_str / from_config) loads one named publisher
  • Publisher.send_json(...) and Publisher.request_json(...) serialize Python JSON values in Rust

The Python surface is synchronous and blocking. Tokio, broker I/O, routing, and batching all stay in Rust.

Config types and schema

mq-bridge-app can create and test route and endpoint JSON/YAML through its UI. It does not replace your Python code or handlers, but it is useful when you want a known-good connection and route shape before pasting the configuration into Python. Load the generated config with Route.from_config, Route.from_yaml, Publisher.from_config, or Publisher.from_yaml.

For the from_config / from_yaml_str mappings, mq_bridge.config ships TypedDict definitions so editors autocomplete the config keys (input, output, batch_size, every transport config, middleware, …):

from mq_bridge import Route
from mq_bridge.config import ConfigDocument

config: ConfigDocument = {
    "routes": {
        "orders": {
            "input": {"memory": {"topic": "orders.in", "capacity": 1600}},
            "output": {"response": {}},
            "batch_size": 128,
        }
    }
}
route = Route.from_config(config, "orders")

These types are generated from the JSON Schema, which the extension produces on demand from the Rust models — there is no checked-in schema copy to drift:

from mq_bridge import config_schema

schema = config_schema()        # the JSON Schema as a dict

config_schema() is handy for editor validation of YAML configs too — dump it to a file and point your # yaml-language-server: $schema= line at it. The types are regenerated with uv run python scripts/gen_config_types.py (a test fails if they drift from the schema).

Running a route

Route.run() blocks the calling thread until another thread calls stop() — it deploys the route and then parks. This is convenient for a process whose only job is the route, but it is a common trap: nothing after route.run() executes until the route stops.

To keep running Python code after the route is up, use start() (non-blocking) or the context-manager form:

route = Route.from_config(config, "orders_route").with_handler(handle)

# Non-blocking: deploys, returns, and runs on a background thread.
route.start()
publisher.send_json({"order_id": 42}, {"kind": "order.created"})
route.stop()
route.join()   # optional: wait for a clean shutdown

# Or scope it to a block — starts on enter, stops + joins on exit:
with Route.from_config(config, "orders_route").with_handler(handle):
    publisher.send_json({"order_id": 42}, {"kind": "order.created"})

Configuration/connection errors surface from start() itself, not from a background thread. run() remains available for the blocking single-route case.

Tuning (environment variables)

These knobs are read from the environment at startup:

Variable Default Effect
MQ_BRIDGE_PY_HANDLER_EXECUTOR worker worker runs handlers on a dedicated interpreter thread that coalesces queued batches under one GIL acquisition (best under load); direct calls the handler inline.
MQ_BRIDGE_PY_HANDLER_CONCURRENCY CPU count Max in-flight handler batches. 0 disables the limit.
MQ_BRIDGE_PY_GC_MODE default default leaves CPython's cyclic GC alone; count disables it and runs gc.collect() every N messages; off disables it entirely (pure refcounting).
MQ_BRIDGE_PY_GC_THRESHOLD 100000 Messages between collections when MQ_BRIDGE_PY_GC_MODE=count.

Local development

uv is a good fit here for the Python-side developer workflow, while maturin stays the build backend:

cd python/mq-bridge-py
uv sync --group dev --no-install-project
uv run maturin develop
uv run pytest -q

Performance smoke tests are skipped by default because they start routes and measure local throughput:

cd python/mq-bridge-py
MQ_BRIDGE_RUN_PERF_TESTS=1 uv run pytest -q -m performance

Examples

Raw message handler:

cd python/mq-bridge-py
uv run python examples/raw_route.py

Kind-based JSON handler:

cd python/mq-bridge-py
uv run python examples/json_route.py

Memory benchmark:

cd python/mq-bridge-py
uv run maturin develop --release
uv run python examples/bench_memory.py --messages 100000

Analysis

HTTP comparison benchmark, driven by a native load generator (wrk) so the client is never the bottleneck. It boots each server itself (mq-bridge in worker and direct executor modes, plus FastAPI, Starlette, Sanic, aiohttp, and FastStream when installed) and drives each with wrk:

cd python/mq-bridge-py
uv run maturin develop --release
uv sync --group bench   # optional Python HTTP peers
uv run python analysis/bench_http_native.py --connections 1,8,32 --duration 8

Requires wrk on PATH (brew install wrk). The FastStream target compares its ASGI custom-route path over Uvicorn; it is not a broker-backed subscriber/publisher benchmark. The examples use included sample configs or create temporary configs.

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

mq_bridge_py-0.2.20.tar.gz (656.3 kB view details)

Uploaded Source

Built Distributions

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

mq_bridge_py-0.2.20-cp38-abi3-win_amd64.whl (23.9 MB view details)

Uploaded CPython 3.8+Windows x86-64

mq_bridge_py-0.2.20-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (23.9 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

mq_bridge_py-0.2.20-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (22.6 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

mq_bridge_py-0.2.20-cp38-abi3-macosx_11_0_arm64.whl (21.4 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

mq_bridge_py-0.2.20-cp38-abi3-macosx_10_12_x86_64.whl (22.5 MB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

Details for the file mq_bridge_py-0.2.20.tar.gz.

File metadata

  • Download URL: mq_bridge_py-0.2.20.tar.gz
  • Upload date:
  • Size: 656.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mq_bridge_py-0.2.20.tar.gz
Algorithm Hash digest
SHA256 ebdd1dfeb7fd833049135c2cbe0b73f703e3ac7751434fb85af769f7370eef5b
MD5 baf41a96d2448db0654d6b602732a5d6
BLAKE2b-256 e68fbc996927f65cdb760fd6c84a33c1a68b5db29b76e52e239a4b448a4d4dd7

See more details on using hashes here.

Provenance

The following attestation bundles were made for mq_bridge_py-0.2.20.tar.gz:

Publisher: publish-python.yml on marcomq/mq-bridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mq_bridge_py-0.2.20-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: mq_bridge_py-0.2.20-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 23.9 MB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mq_bridge_py-0.2.20-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 83646222d00f23f828bc170213ef70e0579a42d8d243f46b880a84f5de3e293d
MD5 f93da484e35349be0c9e48f3f44e5e93
BLAKE2b-256 d28329e80ab0c0782bad2ea443562c42ad9735ae79eaa53a6a2e661a34aa92a1

See more details on using hashes here.

Provenance

The following attestation bundles were made for mq_bridge_py-0.2.20-cp38-abi3-win_amd64.whl:

Publisher: publish-python.yml on marcomq/mq-bridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mq_bridge_py-0.2.20-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mq_bridge_py-0.2.20-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8317f0c5d65e475517f1bc4179f004b5dfb2180519b32c443c8c838d21aed69a
MD5 6b3a6db39a81214dcc4b6a0061ea4495
BLAKE2b-256 d5a475400ffe3e44e5bf345be5d6dc6f5a4ffac436f56790e55d645cce8e90b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for mq_bridge_py-0.2.20-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish-python.yml on marcomq/mq-bridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mq_bridge_py-0.2.20-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for mq_bridge_py-0.2.20-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 518853d90693790c69e13c8f5de583e30e68ac51d921ed6492fd6c45ddab7111
MD5 d3adc1356f773db532f87c1fe381129a
BLAKE2b-256 79bfa7e1b8b4e80b515ff5a50d15de37bf7a6029a956d99f435834e1dd053cb3

See more details on using hashes here.

Provenance

The following attestation bundles were made for mq_bridge_py-0.2.20-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: publish-python.yml on marcomq/mq-bridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mq_bridge_py-0.2.20-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mq_bridge_py-0.2.20-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e5a5ca6e91de76597b451c15d91def7ce28bd3ac43966c53ea4ca8a836c6817f
MD5 3a7c5c8bc8405209671977b25a0a4f83
BLAKE2b-256 94bef6bf255235ea1475b4577e961fb32910a5992a30e0d55bcaf842f21cf7a4

See more details on using hashes here.

Provenance

The following attestation bundles were made for mq_bridge_py-0.2.20-cp38-abi3-macosx_11_0_arm64.whl:

Publisher: publish-python.yml on marcomq/mq-bridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mq_bridge_py-0.2.20-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for mq_bridge_py-0.2.20-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a082d138b368b97f8a8b3a4fd334860c052448986c6b320f170bac72f261ad12
MD5 35baa665d765097c085b7436094dbfe7
BLAKE2b-256 fd5534f29d5afe22ad0e3a9cbbfaef80cd237a6470b34347b1453d333a668c4a

See more details on using hashes here.

Provenance

The following attestation bundles were made for mq_bridge_py-0.2.20-cp38-abi3-macosx_10_12_x86_64.whl:

Publisher: publish-python.yml on marcomq/mq-bridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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