Flask项目配置Celery
service_task/async_tasks/celery_app.py
import os import sys BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) env = os.getenv("env", "dev") path = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) sys.path.insert(0, path) sys.path.append(os.path.dirname(os.path.dirname(__file__))) from flask import Flask from celery import Celery from service_task.create_app_depend import app_conf print("-----------------------------service_task---当前环境-----------------------------") print(env) print("-----------------------------service_task---当前环境-----------------------------") cache_config = app_conf.env_data['RedisCache'] broker = "redis://:{password}@{host}:{port}/0".format(password=cache_config['Password'], host=cache_config['Host'], port=cache_config['Port']) backend = "redis://:{password}@{host}:{port}/1".format(password=cache_config['Password'], host=cache_config['Host'], port=cache_config['Port']) def make_celery(app): celery_app = Celery( main="task_notify", backend=backend, broker=broker, ) celery_app.autodiscover_tasks(['async_tasks.task_relate']) celery_app.conf.task_serializer = "json" celery_app.conf.result_serializer = "json" celery_app.conf.accept_content = ["json"] celery_app.conf.broker_transport_options = {'visibility_timeout': 3600, 'max_retries': 0} celery_app.conf.timezone = "Asia/Shanghai" # 时区 celery_app.conf.enable_utc = False # 是否使用UTC # celery_app.conf.update(app.config) class ContextTask(celery_app.Task): def __call__(self, *args, **kwargs): with app.app_context(): return self.run(*args, **kwargs) celery_app.Task = ContextTask return celery_app falsk_app = Flask(__name__) falsk_app.config.from_object(app_conf.config) from service_task.create_app_depend.sql_db import db # 连接数据库 db.init_app(falsk_app) celery_app = make_celery(falsk_app)
service_task/async_tasks/task_relate.py
from service_task.async_tasks.celery_app import celery_app from service_task.apps.issued_task_school.bll.payment_task_distribution_celery_bll import payment_task_distribution_celery_bll # 学校缴费任务派发 @celery_app.task(name="school_payment_task_distribution", queue="school_task") def school_payment_task_distribution(data): payment_task_distribution_celery_bll.school_payment_task_distribution_process(data) # 机构缴费任务派发 @celery_app.task(name="institution_payment_task_distribution", queue="school_task") def institution_payment_task_distribution(data): payment_task_distribution_celery_bll.institution_payment_task_distribution_process(data) # 再次提醒 @celery_app.task(name="again_distribution_task", queue="school_task") def again_distribution_task(data): payment_task_distribution_celery_bll.again_distribution_task(data)
supervisor/celery配置
启动命令