from flask import Flask
from celery import Celery
from datetime import timedelta
from celery import group, chord, chain
def make_celery(app):
celery = Celery(
app.import_name,
backend=app.config['CELERY_RESULT_BACKEND'],
broker=app.config['CELERY_BROKER_URL']
)
celery.conf.update(app.config)
class ContextTask(celery.Task):
def __call__(self, *args, **kwargs):
with app.app_context():
return self.run(*args, **kwargs)
celery.Task = ContextTask
return celery
app = Flask(__name__)
app.config.update(
CELERY_BROKER_URL='redis://localhost:6379',
CELERY_RESULT_BACKEND='redis://localhost:6379',
)
celery = make_celery(app)
@celery.task()
def add_together(a, b):
print("starting task adding " + str(a) + " & " + str(b))
return a + b
@celery.task()
def finish_callback(results):
print('Group task finished')
return 0
@app.route('/group_task')
def task_group():
short_tasks = [add_together.delay(10, 1),
add_together.delay(10, 3),
add_together.delay(10, 4),
add_together.delay(10, 5),
add_together.delay(10, 6)
]
callback = finish_callback.s()
chord(short_tasks, callback)
return 'Executing Parallel Task'
我有一个Flask应用程序,其中有一些带有子任务的后台操作。我想用芹菜解决这个问题。在上面的代码片段中,和弦未调用回调方法。所有任务均已成功执行,但未执行回调方法。谁能告诉我以上代码有什么问题吗?