芹菜证明存在任务

时间:2018-06-09 14:38:21

标签: python task celery status

我有问题,Celery承诺"待定"队列中的任务以及不存在的任务的状态:请参阅 Celery documentation

我试图用这里提到的解决方案来解决它:Stackoverflow 但这对我没有用。

我有以下设置:

  • Celery 4.1.0
  • Redis 4.0.9
  • Python 3
  • falcon 1.4.1

我的任务定义如下tasks.py

import celery
from celery.signals import after_task_publish

CELERY_BROKER = 'redis://localhost:6379/0'
CELERY_BACKEND = 'redis://localhost:6379/0'

app = celery.Celery('my_app', broker=CELERY_BROKER, backend=CELERY_BACKEND)

@app.task(bind=True, name='testing_async')
def testing_async(self):
    string_result = "This is a async task call"
    return dict(status_code=1, error_message=string_result)

@after_task_publish.connect
def update_sent_state(sender=None, body=None, **kwargs):
    task = app.tasks.get(sender)
    backend = task.backend if task else app.backend
    backend.store_result(body['id'], None, "SENT")

然后在我的views.py中,我称之为:

from celery.result import AsyncResult
from app.tasks import testing_async

class TestAsync(object):
    def on_get(self, req, resp):
        task = testing_async.apply_async()
        resp.status = falcon.HTTP_200
        resp.body = json.dumps(dict(status=task.state, jobid=task.id))

class CheckIdentifierStatus(object):
    def on_get(self, req, resp, task_id):
        task_result = AsyncResult(task_id)
        response = {
            'status': task_result.status,
            'state': task_result.state
        }
        resp.status = falcon.HTTP_200
        resp.body = json.dumps(response)

当我运行我的代码时,我的状态总是"等待",但我预计它会是" SENT"。

{'jobid': '94894297-918d-41d9-824c-591a6b5ea245', 'status': 'PENDING'}
{'status': 'PENDING', 'state': 'PENDING'}
{'status': 'SUCCESS', 'state': 'SUCCESS'}

也许某人有提示,我在这里错了,我在这里看不到状态变化。

编辑1:

此问题现已在芹菜5.0.0版本中计划为GitHub

0 个答案:

没有答案