Unfazed Taskiq
taskiq wrapper with unfazed.
Installation
pip install unfazed-taskiq
Quick Start
This guide will help you get started with Unfazed Taskiq in just a few minutes.
1. Create new db table
CREATE TABLE `unfazed_taskiq_periodic_task` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
`description` text NOT NULL,
`schedule_alias` varchar(255) NOT NULL,
`task_name` varchar(255) NOT NULL,
`task_args` text NOT NULL,
`task_kwargs` text NOT NULL,
`labels` text NOT NULL,
`cron` varchar(255) DEFAULT NULL,
`time` datetime(6) DEFAULT NULL,
`last_run_at` datetime NOT NULL,
`total_run_count` int(11) NOT NULL,
`enabled` tinyint(1) NOT NULL,
`schedule_id` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `idx_schedule_id` (`schedule_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
2. Configure Settings
Add Taskiq configuration to your Unfazed settings file:
AMQP_URL = os.getenv("AMQP_URL", DEFAULT_AMQP_URL)
# entry/settings.py
UNFAZED_TASKIQ_SETTINGS = {
"DEFAULT_TASKIQ_NAME": "default",
"TASKIQ_CONFIG": {
"default": {
"BROKER": {
"BACKEND": "taskiq_aio_pika.AioPikaBroker",
"OPTIONS": {
"url": AMQP_URL,
"exchange_name": "unfazed-taskiq",
"queue_name": "unfazed-taskiq",
},
},
},
},
}
# Add Taskiq lifespan to your Unfazed settings
UNFAZED_SETTINGS = {
"LIFESPAN": ["unfazed_taskiq.lifespan.TaskiqLifeSpan"],
# ... your other settings
}
3. Create Tasks
Define your tasks in your app's tasks.py file:
# app/tasks.py
from unfazed_taskiq.decorators import task
@task
async def add_numbers(a: int, b: int) -> int:
"""Simple addition task."""
return a + b
4. Start Worker
# auto discover all async task
uv run taskiq unfazed-worker unfazed_taskiq.agent:broker -fsd
# auto discover all async task by task file
uv run taskiq unfazed-worker unfazed_taskiq.agent:broker -fsd -tp backend/spider/tasks.py
5. Execute Tasks
from xxx.task import add_numbers
async def your_service():
# Execute task immediately
result = await add_numbers.kiq(10, 20)
print(f"Task result: {result}")
How to use Scheduler
1. Configure Settings
Add Taskiq configuration to your Unfazed settings file:
from unfazed_taskiq.contrib.scheduler.sources import TortoiseScheduleSource
AMQP_URL = os.getenv("AMQP_URL", DEFAULT_AMQP_URL)
unfazedtaskiq_source = TortoiseScheduleSource(schedule_alias="unfazedtaskiq")
unfazedtaskiq_v2_source = TortoiseScheduleSource(schedule_alias="unfazedtaskiq_v2")
# entry/settings.py
UNFAZED_TASKIQ_SETTINGS = {
"DEFAULT_TASKIQ_NAME": "default",
"TASKIQ_CONFIG": {
"default": {
"BROKER": {
"BACKEND": "taskiq_aio_pika.AioPikaBroker",
"OPTIONS": {
"url": AMQP_URL,
"exchange_name": "unfazedtaskiq",
"queue_name": "unfazedtaskiq",
},
"MIDDLEWARES": { # options: if you want use sentry collect error
"unfazed_taskiq.middleware.UnfazedTaskiqExceptionMiddleware"
}
},
"SCHEDULER": {
"SOURCES": [unfazedtaskiq_source],
"BACKEND": "taskiq.TaskiqScheduler",
},
},
"taskiq_task": {
"BROKER": {
"BACKEND": "taskiq_aio_pika.AioPikaBroker",
"OPTIONS": {
"url": AMQP_URL,
"exchange_name": "unfazedtaskiq_v2",
"queue_name": "unfazedtaskiq_v2",
},
"MIDDLEWARES": { # options: if you want use sentry collect error
"unfazed_taskiq.middleware.UnfazedTaskiqExceptionMiddleware"
}
},
"SCHEDULER": {
"SOURCES": [unfazedtaskiq_v2_source],
"BACKEND": "taskiq.TaskiqScheduler",
},
},
},
}
# Add Taskiq lifespan to your Unfazed settings
UNFAZED_SETTINGS = {
"LIFESPAN": ["unfazed_taskiq.lifespan.TaskiqLifeSpan"],
# ... your other settings
}
2. Create Tasks
Define your tasks in your app's tasks.py file:
# app/tasks.py
from unfazed_taskiq.decorators import task
@task
async def send_email(email: str, content: str) -> None:
"""send some msg for some body."""
...
3. Schedule Tasks
You can schedule tasks using the database scheduler:
# Create scheduled tasks in your database
from unfazed_taskiq.contrib.scheduler.models import PeriodicTask
import json
# Create a periodic task that runs every minute
# Also, can use Unfazed-admin UI config
await PeriodicTask.create(
task_name="app.tasks.send_email",
task_args=json.dumps(["test@gmail.com", "test content"]),
task_kwargs=json.dumps({}),
cron="*/1 * * * *", # Every minute
description="Add two numbers every minute",
)
4. Start Scheduler
Execute tasks from your application code:
- start all scheduler
uv run taskiq unfazed-scheduler unfazed_taskiq.agent:scheduler
- start scheduler with alias_name (eg: default / taskiq_task)
uv run taskiq unfazed-scheduler unfazed_taskiq.agent:scheduler --alias-name taskiq_task
5. Start Workers
Start the Taskiq worker to process tasks:
uv run taskiq unfazed-worker unfazed_taskiq.agent:broker -fsd -tp app/tasks.py
Result backend
Result backend is where this library stores Taskiq task results (MySQL/TiDB via MySQLResultBackend): each task gets a row with status, times, args/kwargs, labels, and return value; the full serialized payload is kept in the result column.
Unfazed Admin: add unfazed_taskiq.contrib.result to INSTALLED_APPS. It registers TaskiqResultAdmin with TaskiqResultSerializer, so you can browse and open task runs in the admin UI (list + detail, including a readable return_value field). The raw binary result field is not exposed as JSON in admin APIs.
1. How to enable
Add the app under UNFAZED_SETTINGS["INSTALLED_APPS"], and add the middleware + result backend under TASKIQ_CONFIG for your Taskiq instance name (same place as broker/scheduler):
UNFAZED_SETTINGS = {
# ...
"INSTALLED_APPS": [
# ...your apps...
"unfazed_taskiq.contrib.result",
],
}
UNFAZED_TASKIQ_SETTINGS = {
"TASKIQ_CONFIG": {
"your_taskiq_name": { # e.g. DEFAULT_TASKIQ_NAME
"BROKER": {
# ...broker BACKEND / OPTIONS...
"MIDDLEWARES": [
"unfazed_taskiq.contrib.result.middleware.TaskiqResultPreSendMiddleware",
],
},
"RESULT": {
"BACKEND": "unfazed_taskiq.contrib.result.mysql.MySQLResultBackend",
"OPTIONS": {},
},
# ...SCHEDULER, etc...
},
},
}
2. Create table
CREATE TABLE `taskiq_result` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`task_id` VARCHAR(255) NOT NULL UNIQUE
COMMENT 'Task unique identifier',
`status` SMALLINT NOT NULL
COMMENT 'TaskStatus: 1=STARTED, 2=SUCCESS, 3=FAILURE',
`result` BLOB NULL
COMMENT 'Serialized TaskiqResult from serializer.dumpb',
`return_value` JSON NULL
COMMENT 'Snapshot via encode_for_json_field; full value in result blob',
`date_done` BIGINT NULL
COMMENT 'Timestamp when task completed',
`date_created` BIGINT NULL
COMMENT 'Timestamp when task was enqueued',
`task_name` VARCHAR(255) NULL
COMMENT 'Task definition name',
`schedule_id` VARCHAR(255) NULL
COMMENT 'Schedule id for periodic tasks (from labels)',
`task_args` JSON NULL
COMMENT 'JSON array or __taskiq_json_str_fallback__ str(list)',
`task_kwargs` JSON NULL
COMMENT 'JSON object or __taskiq_json_str_fallback__ str(dict)',
`labels` JSON NULL
COMMENT 'Task labels snapshot recorded',
`traceback` TEXT NULL
COMMENT 'Traceback when task failed',
INDEX `idx_date_done` (`date_done`),
INDEX `idx_task_name_date_done` (`task_name`, `date_done`),
INDEX `idx_schedule_id_date_done` (`schedule_id`, `date_done`),
INDEX `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
For an existing taskiq_result table without labels, run the following SQL or apply the equivalent ORM migration before upgrading the application.
ALTER TABLE `taskiq_result` ADD COLUMN `labels` JSON NULL;
TaskiqResultPreSendMiddleware saves the outgoing message labels before sending to the broker.
3. How to get the result
task = await add_numbers.kiq(10, 20)
value = await task.wait_result() # waits until done, then returns the value
📖 更多文档
pls read taskiq document
📄 许可证
本项目基于 MIT 许可证开源。
Metadata
Release files for unfazed-taskiq 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| unfazed_taskiq-0.1.0.tar.gz | 92.3 kB | Without provenance | Details for unfazed_taskiq-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| unfazed_taskiq-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for unfazed_taskiq-0.1.0-py3-none-any.whl |
Total release size: 117.5 kB
Release files / unfazed_taskiq-0.1.0.tar.gz
| Download URL | unfazed_taskiq-0.1.0.tar.gz |
|---|---|
| Size | 92.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
21004e51352c50f658452f86e481ca6dac14d4317c2816c34d081f77cd7b0ef5
|
|
BLAKE2b-256 checksum How to use checksums |
c7ea28d2cc8843722a48d9d47a4a138d58cd4e7447ff47a95462b19c64911fc9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / unfazed_taskiq-0.1.0-py3-none-any.whl
| Download URL | unfazed_taskiq-0.1.0-py3-none-any.whl |
|---|---|
| Size | 25.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4f2ad122d6f203bcf1d150670b0cc706ff31da339b28e7f78f5e9969db812c61
|
|
BLAKE2b-256 checksum How to use checksums |
207618d0f38561622588f29d133b6a1bfe39f59e284807256973cfd0dc6cc6c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for unfazed-taskiq 0.1.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.