本文為影片筆記,文中內容與影片不一定完全相同。 建立 Flask 官網 提供,整合 Celery 和 Flask 的代碼: # flask_celery.py from celery import Celery def make_celery(app): celery = Celery(app.import_name, backend=app.config['CELERY_BACKEND'], broker=app.config['CELERY_BROKER_URL']) celery.conf.update(app.config) TaskBase = celery.Task class ContextTask(TaskBase): abstract = True def __call__(self, *args, **kwargs): with app.app_context(): return TaskBase.__call__(self, *args, **kwargs) celery.Task = ContextTask return celery 建立 Flask 後端: # celery_example.py from flask import Flask from flask_celery import make_celery app = Flask(__name__) app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0' # 使用 Redis 緩存器 app.config['CELERY_BACKEND'] = 'redis://localhost:6379/0' # 也使用 Redis 作為訊息記錄器 celery = make_celery(app) # 建立 Flask app 的延伸 @app.route("/process/ ") def process(