Trouble getting result from Celery queue

左心房为你撑大大i 提交于 2019-12-11 07:48:36

问题


I have been playing with Celery on Windows 7. Right now, I am going through the Next Steps tutorial: http://docs.celeryproject.org/en/latest/getting-started/next-steps.html

I created a celery.py file:

from __future__ import absolute_import

from celery import Celery

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

# app.conf.update(
#   CELERY_TASK_RESULT_EXPIRES=3600,
# )

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

Then I created a tasks.py file:

from __future__ import absolute_import

from 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)

I then fired up a celery worker in one Powershell. Then in another Powershell I added a couple of integers:

>>> from proj.tasks import add
>>> res = add.delay(2, 2)

In the window running the queue, I got a result right away:

[2014-10-29 09:20:28,875: INFO/MainProcess] Received task: proj.tasks.add[3e5783ef-46a1-44d0-893f-0623e5bc0b09]
[2014-10-29 09:20:28,891: INFO/MainProcess] Task proj.tasks.add[3e5783ef-46a1-44d0-893f-0623e5bc0b09] succeeded in 0.016
0000324249s: 4

However, when I try to retrieve the result in the other window with res.get(), the function just hangs. I've read the tutorial several times and looked on the web and cannot find what the issue is. Could the problem be with using amqp as the backend? I guess amqp sends states as messages instead of storing them.

Oddly enough, if I hit Ctrl+C and query the status of res, I get 'PENDING'.

>>> res.status
'PENDING'

I find this odd because I thought the task was completed. I double checked the ids to make sure.

Looks like the client is configured to use amqp as the backend:

>>> print(res.backend)
<celery.backends.amqp.AMQPBackend object at 0x00000000035C0358>

Looks like ignore_result is set to false.

>>> add
<@task: proj.tasks.add of proj:0x2298390>
>>> add.name
'proj.tasks.add'
>>> add.ignore_result
False

回答1:


Turns out this is a windows issue. For the sake of honesty, I should say I got the answer from here: Celery 'Getting Started' not able to retrieve results; always pending

Basically, pass the worker the --pool=solo flag:

> C:\Python27\Scripts\celery.exe -A messaging.tasks worker --loglevel=info --pool=solo

I'm unsure what the pool implementation controls though.



来源:https://stackoverflow.com/questions/26636033/trouble-getting-result-from-celery-queue

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