I\'ve installed Celery and I\'m trying to test it with the Celery First Steps Doc.
I tried using both Redis and RabbitMQ as brokers and backends, but I can\'t get th
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
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
After modifying of tasks it is necessary to restart celery to reread changes.
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://'