我正在尝试在基于Flask的网络应用程序中使用芹菜,并且monitore它的状态。我的想法是将任务的id存储在会话中,并将其用于状态轮询。
以下是我的tasks.py
的内容from traceback import format_exc
import settings
from celery import Celery, current_task
from celery.utils.log import get_task_logger
from celery.result import AsyncResult
from nimble_auth import make_nimble_req
logger = get_task_logger(__name__)
celery = Celery('tasks')
celery.config_from_object(settings)
@celery.task
def make_work(param1, param2):
try:
# work here
current_task.update_state(state='PROGRESS', meta={'current': page, 'total': total_pages})
except StandardError:
logger.error(format_exc())
def get_asynctask(task_id):
return AsyncResult(task_id)
主应用程序的文件web.py
除外@app.route('/task_state/<task_id>')
def task_state(task_id):
return get_asynctask(task_id).state # from task.py
@app.route('/reset_task')
def reset_task():
del session['celery_task_id']
return redirect(url_for('index'))
@app.route('/run')
def run_task():
task = make_work.delay(1, 2)
session['celery_task_id'] = task.task_id
flash('Task submitted OK!')
return redirect(url_for('index'))
设置文件:
BROKER_URL = 'mongodb://localhost:27017/dramba'
CELERY_RESULT_BACKEND = "mongodb"
CELERY_RESULT_DBURI = 'mongodb://localhost:27017/dramba'
但是当我试图获取任务状态时,我得到NotImplementedError:没有配置后端结果。
完整的堆栈跟踪:
Traceback (most recent call last):
File "/Users/cleg/Projects/Venvs/Dramba/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/cleg/Projects/Venvs/Dramba/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Users/cleg/Projects/Venvs/Dramba/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/Users/cleg/Projects/Venvs/Dramba/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/cleg/Projects/Venvs/Dramba/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/cleg/Projects/Venvs/Dramba/lib/python2.7/site-packages/flask/app.py", line 1344, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/cleg/Projects/GitHub/Dramba/dramba.py", line 13, in task_state
return get_asynctask(task_id).state
File "/Users/cleg/Projects/Venvs/Dramba/lib/python2.7/site-packages/celery/result.py", line 284, in state
return self.backend.get_status(self.id)
File "/Users/cleg/Projects/Venvs/Dramba/lib/python2.7/site-packages/celery/backends/base.py", line 490, in _is_disabled
raise NotImplementedError('No result backend configured. '
NotImplementedError: No result backend configured. Please see the documentation for more information.
我做错了什么?
答案 0 :(得分:1)
您是否设置了CELERY_MONGODB_BACKEND_SETTINGS字典?此页面显示了一个示例:
http://docs.celeryproject.org/en/latest/configuration.html#mongodb-backend-settings
CELERY_RESULT_BACKEND = "mongodb"
CELERY_MONGODB_BACKEND_SETTINGS = {
* "host": "192.168.1.100",
* "port": 30000,
* "database": "mydb",
* "taskmeta_collection": "my_taskmeta_collection",
}
答案 1 :(得分:1)
似乎Flask本身无法从您的文件中正确拾取配置。尝试明确指定它并查看它是否有帮助:http://docs.celeryproject.org/en/latest/userguide/application.html#example-2-using-a-configuration-module
另外,为了确定,在Flast进程的stdout上是否有任何消息?
答案 2 :(得分:1)
你可以这样做:
@app.route('/task_state/<task_id>')
def task_state(task_id):
return celery.result.AsyncResult(task_id).state # from task.py
您需要使用Celery的当前实例 Надоиспользоватьужесуществующийэкземпляр芹菜
答案 3 :(得分:1)
尝试类似......
app = Celery('anytaskname',backend="celery.backends.amqp:AMQPBackend")
它在Windows上使用Django 1.8和Celery 3.1.25以及RabbitMQ代理 doc info