如何在flask项目中使用celery?
项目目录
myflask
│ init.py
├─async_tasks
│ │ create_celery.py
│ │ task_email.py
├─views
│ │ home_main.py
start.py
启动文件 start.py
from myflask import flask_app
if __name__ == '__main__':
flask_app.run()
实例化 flaskapp文件 myflask/__init__.py
from flask import Flask
from myflask.views.home_main import home_bp
from myflask.views.user_main import user_bp
def create_flask_app():
flk_app = Flask(__name__)
flk_app.config.from_object("myflask.config.DevConfig")
flk_app.register_blueprint(home_bp)
flk_app.register_blueprint(user_bp)
return flk_app
flask_app = create_flask_app()
视图函数文件 myflask/views/home_main.py
from flask import jsonify
from flask import Blueprint
from celery.result import AsyncResult
from myflask.async_tasks.task_email import send_email
from myflask.async_tasks.create_celery import celery_app
home_bp = Blueprint("home_bp", __name__)
@home_bp.route("/home")
def home():
"""异步处理任务"""
send_email_task = send_email.delay("lisi1745")
send_email_taskid = send_email_task.id
print(f"send email task id is {send_email_taskid}")
return f"send email task id is {send_email_taskid}"
@home_bp.route("/task/<string:task_id>")
def check_task(task_id):
"""前台定时每3秒查询一次任务结果,直到task状态为done"""
print(task_id)
query_task = AsyncResult(id=task_id, app=celery_app)
task_status = query_task.status
return_data = {"status": None, "data": None, "msg": None}
if task_status in ["PENDING", "RETRY", "STARTED"]:
return_data["status"] = "running"
elif task_status == "FAILURE":
return_data["status"] = "done"
return_data["msg"] = "任务处理失败"
elif task_status == "SUCCESS":
task_result = query_task.get()
return_data["status"] = "done"
return_data["data"] = task_result
return jsonify(return_data)
异步处理函数文件 myflask/async_tasks/task_email.py
import time
from myflask.async_tasks.create_celery import celery_app
@celery_app.task
def send_email(id):
print(f"start to send email to {id}")
time.sleep(5)
return f"send email to {id} ok !"
创建celeryapp文件 myflask/async_tasks/create_celery.py
from celery import Celery
def create_celery_app():
cel_app = Celery("celery",
include=["myflask.async_tasks.task_email", "myflask.async_tasks.task_msg"],
broker="redis://127.0.0.1:6379/1",
backend="redis://127.0.0.1:6379/2")
return cel_app
celery_app = create_celery_app()
命令行启动worker,建议用supervisor管理worker进程
celery -A myflask.async_tasks.create_celery worker -l info -P eventlet
注意:用 supervisor 管理 flask进程,同样也可以管理异步任务worker进程。