Django Celery 教程不返回结果

时间:2023-02-14
本文介绍了Django Celery 教程不返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

UDATE3: 发现了问题.请参阅下面的答案.

UDATE3: found the issue. See the answer below.

UPDATE2: 看来我可能一直在通过 manage.py shell 运行 djcelery 教程来处理自动命名和相关导入问题,见下文.它仍然对我不起作用,但现在我收到新的日志错误消息.见下文.

UPDATE2: It seems I might have been dealing with an automatic naming and relative imports problem by running the djcelery tutorial through the manage.py shell, see below. It is still not working for me, but now I get new log error messages. See below.

更新:我在帖子底部添加了日志.示例任务好像没有注册?

UPDATE: I added the log at the bottom of the post. It seems the example task is not registered?

原帖:

我正在尝试让 django-celery 启动并运行.我无法通过示例.

I am trying to get django-celery up and running. I was not able to get through the example.

我成功安装了rabbitmq,并且顺利完成了教程:http://www.rabbitmq.com/getstarted.html

I installed rabbitmq succesfully and went through the tutorials without trouble: http://www.rabbitmq.com/getstarted.html

然后我尝试阅读 djcelery 教程.

I then tried to go through the djcelery tutorial.

当我运行 python manage.py celeryd -l info 时,我收到以下消息:[任务]- app.module.add[2011-07-27 21:17:19, 990: WARNING/MainProcess] celery@sequoia 已启动.

When I run python manage.py celeryd -l info I get the message: [Tasks] - app.module.add [2011-07-27 21:17:19, 990: WARNING/MainProcess] celery@sequoia has started.

所以看起来不错.我把它放在我的设置文件的顶部:

So that looks good. I put this at the top of my settings file:

import djcelery
djcelery.setup_loader()

BROKER_HOST = "localhost"
BROKER_PORT = 5672
BROKER_USER = "guest"
BROKER_PASSWORD = "guest"
BROKER_VHOST = "/"

将这些添加到我安装的应用程序中:

added these to my installed apps:

'djcelery',

这是我的应用任务文件夹中的 tasks.py 文件:

here is my tasks.py file in the tasks folder of my app:

from celery.task import task

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

我将此添加到我的 django.wsgi 文件中:

I added this to my django.wsgi file:

os.environ["CELERY_LOADER"] = "django"

然后我在命令行输入这个:

Then I entered this at the command line:

>>> from app.module.tasks import add
>>> result = add.delay(4,4)
>>> result
(AsyncResult: 7auathu945gry48- a bunch of stuff)
>>> result.ready()
False

看起来它有效,但问题是:

So it looks like it worked, but here is the problem:

>>> result.result
>>>               (nothing is returned)
>>> result.get()

当我输入 result.get() 时,它会挂起.我做错了什么?

When I put in result.get() it just hangs. What am I doing wrong?

更新:当我启动工作服务器时,这就是在前台运行记录器的内容:

UPDATE: This is what running the logger in the foreground says when I start up the worker server:

No handlers could be found for logger "multiprocessing"

[Configuration]
- broker:      amqplib://guest@localhost:5672/
- loader:      djcelery.loaders.DjangoLoader
- logfile:     [stderr]@INFO
- concurrency: 4
- events:      OFF
- beat:        OFF

[Queues]
- celery:      exchange: celery (direct)  binding: celery

[Tasks]
 - app.module.add
[2011-07-27 21:17:19, 990: WARNING/MainProcess] celery@sequoia has started.

 C:Python27libsite-packagesdjango-celery-2.2.4-py2.7.eggdjceleryloaders.py:80:  UserWarning: Using settings.DEBUG leads to a memory leak, neveruse this setting in production environments!
     warnings.warn("Using settings.DEBUG leads to a memory leak, never"

然后当我输入命令时:

>>> result = add(4,4)

这出现在错误日志中:

[2011-07-28 11:00:39, 352: ERROR/MainProcess] Unknown task ignored: Task of kind ‘task.add’ is not registered, please make sure it’s imported. Body->"{‘retries’: 0, ‘task’: ‘tasks.add’, ‘args’: (4,4), ‘expires’: None, ‘ta’: None
    ‘kwargs’: {}, ‘id’: ‘225ec0ad-195e-438b-8905-ce28e7b6ad9’}"
Traceback (most recent call last):
   File "C:Python27..celeryworkerconsumer.py",line 368, in receive_message
      Eventer=self.event_dispatcher)
   File "C:Python27..celeryworkerjob.py",line 306, in from_message 
       **kw)
   File "C:Python27..celeryworkerjob.py",line 275, in __init__
       self.task = tasks[self.task_name]
   File "C:Python27...celery
egistry.py", line 59, in __getitem__
       Raise self.NotRegistered(key)
NotRegistered: ‘tasks.add’   

如何正确注册和处理此任务?谢谢.

How do I get this task to be registered and handled properly? thanks.

更新 2:

此链接表明未注册错误可能是由于客户端和工作人员之间的任务名称不匹配 - http://celeryproject.org/docs/userguide/tasks.html#automatic-naming-and-relative-imports

This link suggested that the not registered error can be due to task name mismatches between client and worker - http://celeryproject.org/docs/userguide/tasks.html#automatic-naming-and-relative-imports

退出manage.py shell,进入python shell,输入如下:

exited the manage.py shell and entered a python shell and entered the following:

>>> from app.module.tasks import add
>>> result = add.delay(4,4)
>>> result.ready()
False
>>> result.result
>>>                 (nothing returned)
>>> result.get()
                    (it just hangs there)

所以我得到了相同的行为,但是新的日志消息.从日志中可以看出,服务器正在工作,但它不会反馈结果:

so I am getting the same behavior, but new log message. From the log, it appears the server is working but it won't feed the result back out:

[2011-07-28 11:39:21, 706: INFO/MainProcess] Got task from broker: app.module.tasks.add[7e794740-63c4-42fb-acd5-b9c6fcd545c3]
[2011-07-28 11:39:21, 706: INFO/MainProcess] Task app.module.tasks.add[7e794740-63c4-42fb-acd5-b9c6fcd545c3] succeed in 0.04600000038147s: 8

所以服务器得到了任务并计算了正确的答案,但它不会发回?为什么不呢?

So the server got the task and it computed the correct answer, but it won't send it back? why not?

推荐答案

我从另一个 stackoverflow 帖子中找到了我的问题的解决方案:为什么 Celery 在 Python shell 中有效,但在我的 Django 视图中无效?(导入问题)

I found the solution to my problem from another stackoverflow post: Why does Celery work in Python shell, but not in my Django views? (import problem)

我必须将这些行添加到我的设置文件中:

I had to add these lines to my settings file:

CELERY_RESULT_BACKEND = "amqp"
CELERY_IMPORTS = ("app.module.tasks", )

然后在 task.py 文件中,我将任务命名为:

then in the task.py file I named the task as such:

@task(name="module.tasks.add")

必须将任务名称通知服务器和客户端.celery 和 django-celery 教程在他们的教程中省略了这些行.

The server and the client had to be informed of the task names. The celery and django-celery tutorials omit these lines in their tutorials.

这篇关于Django Celery 教程不返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:使用 pika 在 python 中的 SparkStreaming、RabbitMQ 和 MQTT 下一篇:SockJS Python 客户端

相关文章

最新文章