How taskiq-python/taskiq works
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: receiver.py Receiver · broker.py Broker ABC · kicker.py Kicker · scheduler.py Scheduler
Components 22
Core API
Task declaration, invocation, message model, and shared execution context.
- Public API · Top-level import surface for the library.
taskiq/__init__.py - Task Decorator · Creates user-facing task wrappers around Python callables.
taskiq/decor.py - Task Wrapper · Holds task metadata and the .kiq invocation path.
taskiq/task.py - Kicker · Turns task calls into broker messages for asynchronous execution.
taskiq/kicker.py - Message Model · Defines the structured message payload passed between producer and worker.
taskiq/message.py
Transport
Broker abstractions and bundled local transports.
- Broker ABC · Defines the broker interface used by producers and workers.
taskiq/abc/broker.py - In-memory Broker · Ephemeral broker for testing and local execution.
taskiq/brokers/inmemory_broker.py - Shared Broker · Shared-process broker used for lightweight coordination.
taskiq/brokers/shared_broker.py - ZMQ Broker · ZeroMQ-based bundled transport implementation.
taskiq/brokers/zmq_broker.py
Worker Runtime
Message reception, parameter parsing, and task execution.
- Receiver · Consumes broker messages and drives execution.
taskiq/receiver/receiver.py - Params Parser · Reconstructs task arguments and execution parameters from messages.
taskiq/receiver/params_parser.py
Scheduling
Schedule sources and scheduled-task logic.
- Scheduler · Collects due schedules and emits task messages.
taskiq/scheduler/scheduler.py - Label Source · Built-in label-based source for scheduled tasks.
taskiq/schedule_sources/label_based.py - Scheduled Task · Versioned scheduled-task definitions and validation logic.
taskiq/scheduler/scheduled_task/v2.py
Results
Result abstractions and storage backends.
- Result API · Represents task results independently from transport.
taskiq/result/result.py - Dummy 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 · Starts the worker process and operational helpers.
taskiq/cli/worker/cmd.py - Scheduler CLI · Starts the scheduler process.
taskiq/cli/scheduler/cmd.py
Cross-cutting
Serialization, middleware, formatting, and dependencies.
- Serialization · Coordinates encoding and decoding for task messages and payloads.
taskiq/serialization.py - Codecs · Built-in JSON, MsgPack, CBOR, ORJSON, and Pickle serializers.
taskiq/serializers/__init__.py - Middleware Stack · Retry, telemetry, metrics, and admin hooks around execution.
taskiq/middlewares/__init__.py - State & Deps · Shared execution state, dependency trees, and context helpers.
taskiq/state.py
Connections 25
- 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.