Celery error : result.get times out

◇◆丶佛笑我妖孽 提交于 2019-12-01 06:55:07

After modifying of tasks it is necessary to restart celery to reread changes.

Finally worked with this project layout

proj/celery_proj/__init__.py
                /celery.py
                /tasks.py
    /test.py

Where

celery.py

from __future__ import absolute_import

from celery import Celery

app = Celery('celery_proj',
             broker='amqp://',
             backend='amqp://',
             include=['celery_proj.tasks'])

# Optional configuration, see the application user guide.
app.conf.update(
    CELERY_TASK_RESULT_EXPIRES=3600,
)

if __name__ == '__main__':
    app.start()

tasks.py

from __future__ import absolute_import

from celery_proj.celery import app


@app.task
def add(x, y):
    return x + y


@app.task
def mul(x, y):
    return x * y


@app.task
def xsum(numbers):
    return sum(numbers)

test.py

__author__ = 'mehdi'
path = '/home/mehdi/PycharmProjects'
import sys
sys.path.append(path)
from celery_proj.tasks import add

r = add.delay(4,4)
print(r.status)
print(r.result)

And launching the worker with :

cd proj
celery -A celery_proj worker -l info

An then running test.py :

python test.py

The 'backend' configuration can no longer be passed to the Celery object as an optional parameter, but should be passed through the python configuration parameter CELERY_RESULT_BACKEND (see https://github.com/celery/celery/issues/2146).

So the tasks.py (from the Celery tutorial) should look something like:

from celery import Celery

app = Celery('tasks', broker='amqp://guest@localhost//')
app.config_from_object('celeryconfig')

@app.task
def add(x, y):
    print '[' + str(x) + '][' + str(y) + ']=' + str(x+y)
    return x + y

Create a file celeryconfig.py in the same directory as tasks.py with the following content:

CELERY_RESULT_BACKEND='amqp://'

Using app.backend.get_result(result.id) to instead of AsyncResult.get() since AsyncResult.get() will block until the task status become ready, however the task has already run completed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!