Celery
Celery
Section titled “Celery”Celeryは、Pythonの分散タスクキューシステムです。FastAPIと組み合わせて、非同期タスクを処理できます。
Celeryの設定
Section titled “Celeryの設定”依存関係の追加
Section titled “依存関係の追加”pip install celery redisCeleryアプリの設定
Section titled “Celeryアプリの設定”from celery import Celery
celery_app = Celery( "tasks", broker="redis://localhost:6379/0", backend="redis://localhost:6379/0")
@celery_app.taskdef send_email(email: str, message: str): # メール送信処理 passFastAPIとの統合
Section titled “FastAPIとの統合”from fastapi import FastAPIfrom .celery_app import celery_app, send_email
app = FastAPI()
@app.post("/send-email")async def send_email_endpoint(email: str, message: str): send_email.delay(email, message) return {"status": "Email queued"}