# taskiq-python/taskiq architecture

> Architecture diagram of the GitHub repository taskiq-python/taskiq, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/taskiq-python/taskiq
- Repository: https://github.com/taskiq-python/taskiq

## Overview

1. **What this repository is**
   - `taskiq` is a Python package for asynchronous distributed task queues.
   - It is a library, not an application: the repo contains the core runtime, CLI entrypoints, abstractions, built-in transports, tests, and docs.

2. **Core runtime boundary**
   - The public API lives mostly under `taskiq/` and is centered around task declaration, task invocation, message transport, task execution, scheduling, and results.
   - The top-level package exposes the user-facing entry points; the rest is split into transport-agnostic core logic and transport-specific adapters.

3. **Task definition and invocation path**
   - A task is usually defined by decorating a Python function with a broker-provided decorator.
   - When `.kiq(...)` is called, the task call is turned into a message, serialized, and sent through the broker rather than executed inline.
   - This is the main producer-side flow: application code → task wrapper/kicker → serialized message → broker.

4. **Broker abstraction and transport layer**
   - `taskiq/abc/broker.py` defines the broker contract.
   - Built-in broker implementations in this repo are limited to local/testing-oriented transports such as `inmemory_broker`, `shared_broker`, and `zmq_broker`.
   - Production backends such as Redis, NATS, RabbitMQ, or Kafka are external packages; the core repo is designed to be transport-agnostic around the broker interface.

5. **Worker-side execution pipeline**
   - The worker process is driven by the receiver subsystem in `taskiq/receiver/`.
   - The receiver pulls messages from the broker, parses parameters, reconstructs the task call, applies middleware, and executes the target function.
   - This is the consumer-side boundary: broker message → receiver → dependency/state resolution → task execution → result handling.

6. **Serialization and message format**
   - Serialization is separated from transport via `taskiq/serializers/` and `taskiq/serialization.py`.
   - Multiple codecs are supported, including JSON, MsgPack, CBOR, ORJSON, and Pickle.
   - `taskiq/message.py` and related modules define the message structure that brokers move between producer and worker.

7. **Result handling**
   - Results are abstracted from execution via `taskiq/result/` and `taskiq/result_backends/`.
   - The repo includes a dummy backend; the abstraction allows task results to be stored or retrieved through external backends without changing task code.
   - Result handling is a distinct subsystem from message transport, so execution and result persistence can evolve independently.

8. **Middleware and cross-cutting concerns**
   - `taskiq/middlewares/` contains the execution pipeline extensions: retry behavior, OpenTelemetry instrumentation, Prometheus metrics, and admin-related middleware.
   - Middleware is the main place where cross-cutting behavior is injected around task execution without coupling it to the task body or broker implementation.

9. **Scheduling subsystem**
   - Scheduling is implemented under `taskiq/scheduler/` and `taskiq/schedule_sources/`.
   - The scheduler produces due task messages from one or more schedule sources; the label-based source is included in-core.
   - The scheduled task model supports multiple versions and cron/interval/time validation, making scheduling a separate producer of task messages alongside application code.

10. **CLI/runtime entrypoints**
   - The command-line interface is split into worker and scheduler commands under `taskiq/cli/`.
   - `taskiq worker ...` starts the worker process; `taskiq scheduler ...` starts the schedule runner.
   - CLI code also handles process management, file watching/reload support, argument parsing, and log collection, so operational concerns are isolated from the core library.

11. **State, dependencies, and execution context**
   - `taskiq/context.py`, `taskiq/state.py`, `taskiq/depends/`, and parts of the receiver pipeline provide shared execution state and dependency-style injection.
   - The docs and examples show this as a first-class feature: tasks can reuse state, dependency trees, generators, and framework-provided contexts.
   - This is an important boundary because it lets task execution integrate with async frameworks and application state without hard-coding framework logic into the queue core.

12. **Testing, docs, and project tooling**
   - The repo is heavily covered by tests under `tests/`, with focused suites for brokers, receiver, scheduler, serializers, middleware, CLI, and instrumentation.
   - Documentation is a separate VuePress site under `docs/`, with examples that mirror the architecture: broker usage, scheduling, middleware extension, state/deps, and framework integrations.
   - GitHub Actions, pre-commit, and packaging files support release, docs publishing, and validation, but they are auxiliary to the runtime architecture.

Main entry points: [`taskiq/receiver/receiver.py`](https://github.com/taskiq-python/taskiq/blob/master/taskiq/receiver/receiver.py) (Receiver), [`taskiq/abc/broker.py`](https://github.com/taskiq-python/taskiq/blob/master/taskiq/abc/broker.py) (Broker ABC), [`taskiq/kicker.py`](https://github.com/taskiq-python/taskiq/blob/master/taskiq/kicker.py) (Kicker), [`taskiq/scheduler/scheduler.py`](https://github.com/taskiq-python/taskiq/blob/master/taskiq/scheduler/scheduler.py) (Scheduler)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core API"]
  node_pkg["Public API<br/>package entrypoint<br/>[__init__.py]"]
  node_decor["Task Decorator<br/>task definition<br/>[decor.py]"]
  node_task["Task Wrapper<br/>task call wrapper<br/>[task.py]"]
  node_kicker["Kicker<br/>message producer<br/>[kicker.py]"]
  node_message["Message Model<br/>task message<br/>[message.py]"]
end

subgraph group_transport["Transport"]
  node_broker_abc["Broker ABC<br/>abstract contract<br/>[broker.py]"]
  node_inmemory["In-memory Broker<br/>local transport<br/>[inmemory_broker.py]"]
  node_shared["Shared Broker<br/>local transport<br/>[shared_broker.py]"]
  node_zmq["ZMQ Broker<br/>local transport<br/>[zmq_broker.py]"]
end

subgraph group_worker["Worker Runtime"]
  node_receiver["Receiver<br/>worker loop<br/>[receiver.py]"]
  node_params["Params Parser<br/>call binding<br/>[params_parser.py]"]
end

subgraph group_schedule["Scheduling"]
  node_scheduler["Scheduler<br/>schedule runner<br/>[scheduler.py]"]
  node_schedule_source["Label Source<br/>schedule source<br/>[label_based.py]"]
  node_scheduled_task["Scheduled Task<br/>schedule model<br/>[v2.py]"]
end

subgraph group_results["Results"]
  node_result["Result API<br/>result abstraction<br/>[result.py]"]
  node_dummy_backend["Dummy Backend<br/>result backend<br/>[dummy.py]"]
end

subgraph group_ops["CLI & Ops"]
  node_worker_cli["Worker CLI<br/>command entrypoint<br/>[cmd.py]"]
  node_scheduler_cli["Scheduler CLI<br/>command entrypoint<br/>[cmd.py]"]
end

subgraph group_cross["Cross-cutting"]
  node_serialization["Serialization<br/>codec routing<br/>[serialization.py]"]
  node_serializers["Codecs<br/>serializer set<br/>[__init__.py]"]
  node_middlewares["Middleware Stack<br/>execution hooks<br/>[__init__.py]"]
  node_state["State & Deps<br/>injection context<br/>[state.py]"]
end

node_pkg -->|"exports"| node_decor
node_decor -->|"wraps"| node_task
node_task -->|"invokes"| node_kicker
node_kicker -->|"builds"| node_message
node_kicker -->|"encodes"| node_serialization
node_serialization -->|"uses"| node_serializers
node_kicker -->|"sends via"| node_broker_abc
node_inmemory -->|"implements"| node_broker_abc
node_shared -->|"implements"| node_broker_abc
node_zmq -->|"implements"| node_broker_abc
node_broker_abc -->|"feeds"| node_receiver
node_receiver -->|"parses"| node_params
node_receiver -->|"wraps"| node_middlewares
node_receiver -->|"binds"| node_state
node_receiver -->|"reports"| node_result
node_result -->|"stores via"| node_dummy_backend
node_scheduler -->|"reads"| node_schedule_source
node_scheduler -->|"evaluates"| node_scheduled_task
node_scheduler -->|"emits"| node_message
node_scheduler_cli -->|"starts"| node_scheduler
node_worker_cli -->|"starts"| node_receiver
node_worker_cli -->|"configures"| node_middlewares
node_worker_cli -->|"initializes"| node_state
node_serialization -->|"formats"| node_message
node_middlewares -->|"uses"| node_state

click node_pkg "https://github.com/taskiq-python/taskiq/blob/master/taskiq/__init__.py"
click node_decor "https://github.com/taskiq-python/taskiq/blob/master/taskiq/decor.py"
click node_task "https://github.com/taskiq-python/taskiq/blob/master/taskiq/task.py"
click node_kicker "https://github.com/taskiq-python/taskiq/blob/master/taskiq/kicker.py"
click node_message "https://github.com/taskiq-python/taskiq/blob/master/taskiq/message.py"
click node_serialization "https://github.com/taskiq-python/taskiq/blob/master/taskiq/serialization.py"
click node_serializers "https://github.com/taskiq-python/taskiq/blob/master/taskiq/serializers/__init__.py"
click node_broker_abc "https://github.com/taskiq-python/taskiq/blob/master/taskiq/abc/broker.py"
click node_inmemory "https://github.com/taskiq-python/taskiq/blob/master/taskiq/brokers/inmemory_broker.py"
click node_shared "https://github.com/taskiq-python/taskiq/blob/master/taskiq/brokers/shared_broker.py"
click node_zmq "https://github.com/taskiq-python/taskiq/blob/master/taskiq/brokers/zmq_broker.py"
click node_receiver "https://github.com/taskiq-python/taskiq/blob/master/taskiq/receiver/receiver.py"
click node_params "https://github.com/taskiq-python/taskiq/blob/master/taskiq/receiver/params_parser.py"
click node_middlewares "https://github.com/taskiq-python/taskiq/blob/master/taskiq/middlewares/__init__.py"
click node_state "https://github.com/taskiq-python/taskiq/blob/master/taskiq/state.py"
click node_result "https://github.com/taskiq-python/taskiq/blob/master/taskiq/result/result.py"
click node_dummy_backend "https://github.com/taskiq-python/taskiq/blob/master/taskiq/result_backends/dummy.py"
click node_scheduler "https://github.com/taskiq-python/taskiq/blob/master/taskiq/scheduler/scheduler.py"
click node_schedule_source "https://github.com/taskiq-python/taskiq/blob/master/taskiq/schedule_sources/label_based.py"
click node_scheduled_task "https://github.com/taskiq-python/taskiq/blob/master/taskiq/scheduler/scheduled_task/v2.py"
click node_worker_cli "https://github.com/taskiq-python/taskiq/blob/master/taskiq/cli/worker/cmd.py"
click node_scheduler_cli "https://github.com/taskiq-python/taskiq/blob/master/taskiq/cli/scheduler/cmd.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_pkg,node_decor,node_task,node_kicker,node_message,node_serialization,node_serializers,node_middlewares,node_state toneBlue
class node_broker_abc,node_inmemory,node_shared,node_zmq toneAmber
class node_receiver,node_params toneMint
class node_scheduler,node_schedule_source,node_scheduled_task toneRose
class node_result,node_dummy_backend toneIndigo
class node_worker_cli,node_scheduler_cli toneTeal
```

## Components

### Core API

Task declaration, invocation, message model, and shared execution context.

- [Public API](https://github.com/taskiq-python/taskiq/blob/master/taskiq/__init__.py): package entrypoint · Top-level import surface for the library. · `taskiq/__init__.py`
- [Task Decorator](https://github.com/taskiq-python/taskiq/blob/master/taskiq/decor.py): task definition · Creates user-facing task wrappers around Python callables. · `taskiq/decor.py`
- [Task Wrapper](https://github.com/taskiq-python/taskiq/blob/master/taskiq/task.py): task call wrapper · Holds task metadata and the .kiq invocation path. · `taskiq/task.py`
- [Kicker](https://github.com/taskiq-python/taskiq/blob/master/taskiq/kicker.py): message producer · Turns task calls into broker messages for asynchronous execution. · `taskiq/kicker.py`
- [Message Model](https://github.com/taskiq-python/taskiq/blob/master/taskiq/message.py): task message · Defines the structured message payload passed between producer and worker. · `taskiq/message.py`

### Transport

Broker abstractions and bundled local transports.

- [Broker ABC](https://github.com/taskiq-python/taskiq/blob/master/taskiq/abc/broker.py): abstract contract · Defines the broker interface used by producers and workers. · `taskiq/abc/broker.py`
- [In-memory Broker](https://github.com/taskiq-python/taskiq/blob/master/taskiq/brokers/inmemory_broker.py): local transport · Ephemeral broker for testing and local execution. · `taskiq/brokers/inmemory_broker.py`
- [Shared Broker](https://github.com/taskiq-python/taskiq/blob/master/taskiq/brokers/shared_broker.py): local transport · Shared-process broker used for lightweight coordination. · `taskiq/brokers/shared_broker.py`
- [ZMQ Broker](https://github.com/taskiq-python/taskiq/blob/master/taskiq/brokers/zmq_broker.py): local transport · ZeroMQ-based bundled transport implementation. · `taskiq/brokers/zmq_broker.py`

### Worker Runtime

Message reception, parameter parsing, and task execution.

- [Receiver](https://github.com/taskiq-python/taskiq/blob/master/taskiq/receiver/receiver.py): worker loop · Consumes broker messages and drives execution. · `taskiq/receiver/receiver.py`
- [Params Parser](https://github.com/taskiq-python/taskiq/blob/master/taskiq/receiver/params_parser.py): call binding · Reconstructs task arguments and execution parameters from messages. · `taskiq/receiver/params_parser.py`

### Scheduling

Schedule sources and scheduled-task logic.

- [Scheduler](https://github.com/taskiq-python/taskiq/blob/master/taskiq/scheduler/scheduler.py): schedule runner · Collects due schedules and emits task messages. · `taskiq/scheduler/scheduler.py`
- [Label Source](https://github.com/taskiq-python/taskiq/blob/master/taskiq/schedule_sources/label_based.py): schedule source · Built-in label-based source for scheduled tasks. · `taskiq/schedule_sources/label_based.py`
- [Scheduled Task](https://github.com/taskiq-python/taskiq/blob/master/taskiq/scheduler/scheduled_task/v2.py): schedule model · Versioned scheduled-task definitions and validation logic. · `taskiq/scheduler/scheduled_task/v2.py`

### Results

Result abstractions and storage backends.

- [Result API](https://github.com/taskiq-python/taskiq/blob/master/taskiq/result/result.py): result abstraction · Represents task results independently from transport. · `taskiq/result/result.py`
- [Dummy Backend](https://github.com/taskiq-python/taskiq/blob/master/taskiq/result_backends/dummy.py): result backend · Built-in no-op backend for result storage abstraction. · `taskiq/result_backends/dummy.py`

### CLI & Ops

Worker and scheduler entrypoints plus operational helpers.

- [Worker CLI](https://github.com/taskiq-python/taskiq/blob/master/taskiq/cli/worker/cmd.py): command entrypoint · Starts the worker process and operational helpers. · `taskiq/cli/worker/cmd.py`
- [Scheduler CLI](https://github.com/taskiq-python/taskiq/blob/master/taskiq/cli/scheduler/cmd.py): command entrypoint · Starts the scheduler process. · `taskiq/cli/scheduler/cmd.py`

### Cross-cutting

Serialization, middleware, formatting, and dependencies.

- [Serialization](https://github.com/taskiq-python/taskiq/blob/master/taskiq/serialization.py): codec routing · Coordinates encoding and decoding for task messages and payloads. · `taskiq/serialization.py`
- [Codecs](https://github.com/taskiq-python/taskiq/blob/master/taskiq/serializers/__init__.py): serializer set · Built-in JSON, MsgPack, CBOR, ORJSON, and Pickle serializers. · `taskiq/serializers/__init__.py`
- [Middleware Stack](https://github.com/taskiq-python/taskiq/blob/master/taskiq/middlewares/__init__.py): execution hooks · Retry, telemetry, metrics, and admin hooks around execution. · `taskiq/middlewares/__init__.py`
- [State & Deps](https://github.com/taskiq-python/taskiq/blob/master/taskiq/state.py): injection context · Shared execution state, dependency trees, and context helpers. · `taskiq/state.py`

## Connections

- Public API → Task Decorator: exports · Public API exposes task decoration and task creation.
- Task Decorator → Task Wrapper: wraps · Decorator builds the task wrapper around a callable.
- Task Wrapper → Kicker: invokes · Task wrapper routes .kiq calls into the producer path.
- Kicker → Message Model: builds · Task invocation becomes a structured message.
- Kicker → Serialization: encodes · Payloads are serialized before leaving the producer.
- Serialization → Codecs: uses · Serialization dispatches to concrete codecs.
- Kicker → Broker ABC: sends via · Producer-side emission targets the broker contract.
- In-memory Broker → Broker ABC: implements · In-memory transport satisfies the broker abstraction.
- Shared Broker → Broker ABC: implements · Shared broker satisfies the broker abstraction.
- ZMQ Broker → Broker ABC: implements · ZMQ broker satisfies the broker abstraction.
- Broker ABC → Receiver: feeds · Workers consume messages through the broker interface.
- Receiver → Params Parser: parses · Receiver reconstructs call parameters from broker messages.
- Receiver → Middleware Stack: wraps · Middleware surrounds task execution.
- Receiver → State & Deps: binds · Execution uses shared state and dependency resolution.
- Receiver → Result API: reports · Execution produces result objects through the result layer.
- Result API → Dummy Backend: stores via · Result abstraction can delegate to a backend implementation.
- Scheduler → Label Source: reads · Scheduler gathers due work from schedule sources.
- Scheduler → Scheduled Task: evaluates · Scheduler relies on task schedule models and validation.
- Scheduler → Message Model: emits · Due schedules are converted into task messages.
- Scheduler CLI → Scheduler: starts · CLI entrypoint launches the scheduler loop.
- Worker CLI → Receiver: starts · CLI entrypoint launches the worker runtime.
- Worker CLI → Middleware Stack: configures · Worker startup wires operational middleware and hooks.
- Worker CLI → State & Deps: initializes · Worker process sets up execution context and shared state.
- Serialization → Message Model: formats · Serialization operates on the task message structure.
- Middleware Stack → State & Deps: uses · Middleware can read and extend execution state.
