Taskiq Plugin for Unfazed
Project description
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. 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
}
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 add_numbers(a: int, b: int) -> int:
"""Simple addition task."""
return a + b
3. 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
4. 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
📖 更多文档
pls read taskiq document
📄 许可证
本项目基于 MIT 许可证开源。
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file unfazed_taskiq-0.0.5.tar.gz.
File metadata
- Download URL: unfazed_taskiq-0.0.5.tar.gz
- Upload date:
- Size: 70.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03e919bc0982b82bb92c9a7916713130912ce161e5f256b746c76545b99208c4
|
|
| MD5 |
570912047db3073695df9339bae8582f
|
|
| BLAKE2b-256 |
32200497cbcc7364f44c4e9680dfef6aa18797fe946de8c56d10e9c5263fcf88
|
File details
Details for the file unfazed_taskiq-0.0.5-py3-none-any.whl.
File metadata
- Download URL: unfazed_taskiq-0.0.5-py3-none-any.whl
- Upload date:
- Size: 17.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e88332232a2522e9f3cebf417b897908b9234575d66450cf2a4b5cb146d7de0
|
|
| MD5 |
2ed56479aa33d55b05b79e69ec2b892b
|
|
| BLAKE2b-256 |
feb52b8538d8f2f77c3682d46caa2ebcbb954b1108e35599cc00b9a876898d1a
|