Celery - completes a task but never returns a result

I just installed Celery and try to follow the guide:

I have a file called tasks.py with the following code:

from celery import Celery app = Celery('tasks', backend='amqp', broker='amqp://') @app.task def add(x, y): return x + y 

I installed RabitMQ (I did not configure it, since the tutorial did not mention anything like that).

I start the server of the working celery server as follows:

 celery -A tasks worker --loglevel=info 

It seems to start normally (here is the output: http://i.imgur.com/qnoNCzJ.png )

Then I ran a script with the following:

 from tasks import add from time import sleep result = add.delay(2,2) while not result.ready(): sleep(10) 

When I check result.ready() , I always get False (so the while loop above works forever). However, on celery magazines everything looks fine:

 [2014-10-30 00:58:46,673: INFO/MainProcess] Received task: tasks.add[2bc4ceba-1319-49ce-962d-1ed0a424a2ce] [2014-10-30 00:58:46,674: INFO/MainProcess] Task tasks.add[2bc4ceba-1319-49ce-962d-1ed0a424a2ce] succeeded in 0.000999927520752s: 4 

So, the task was restored and succeeded. However, result.ready() is still false. Is it possible to understand why this could be? I am on Windows 7 and I am using RabbitMQ. Thanks in advance.

+6
source share
3 answers

Need to solve your problem

 ignore_result=False 
+3
source

Ok, I installed a clean virtual machine with fresh celery installed, installed the following files:

tasks.py:

 from celery import Celery app = Celery('tasks', backend='amqp', broker='amqp://') @app.task def add(x, y): return x + y 

And runme.py

 from tasks import add import time result = add.delay(1,2) while not result.ready(): time.sleep(1) print(result.get()) 

Then I installed celery with:

 celery -A tasks worker --loglevel=info 

And subsequently I run runme.py, which gives the expected result:

 [ puciek@somewhere tmp]# python3.3 runme.py 3 

So, the problem with your setup is most likely somewhere in the rabbit-mq installation, so I recommend reinstalling it with the latest stable version from the sources that I use, and as you can see, it works just fine.

Update:

Actually, your problem can be as trivial as imaginable - are you sure that you are using the same version to launch celery and using your consumer? I just managed to play it, where I ran celery in Python3.3 and then run runme.py with version 2.7. The result was as you described.

+2
source

Source: https://habr.com/ru/post/977455/


All Articles