Django Celery教程不返回结果 [英] Django Celery tutorial not returning results

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

问题描述

UDATE3:发现问题。请看下面的答案。



更新2:似乎我可能通过运行djcelery教程来处理自动命名和相对导入问题manage.py shell,请参见下文。它仍然不适用于我,但现在我收到新的日志错误消息。



更新:我将日志添加到帖子的底部。似乎示例任务没有注册?



原始帖子



我我试图让django-celery开始运行。我没有能够通过这个例子。



我成功安装了rabbitmq,没有麻烦地浏览教程: http://www.rabbitmq.com/getstarted.html



然后我试图去通过djcelery教程。



当我运行 python manage.py celeryd -l info 我收到消息:
[任务]
- app.module.add
[2011-07-27 21:17:19,990:WARNING / MainProcess]芹菜@红杉已经开始了。



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

  import djcelery 
djcelery.setup_loader()

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

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

 'djcelery',

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

  from celery.task import task 

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

我把它添加到我的django.wsgi文件:

  os.environ [CELERY_LOADER] =django
/ pre>

然后我在命令行输入:

  >>>来自app.module.tasks import add 
>>> result = add.delay(4,4)
>>>结果
(AsyncResult:7auathu945gry48-一堆东西)
>>> result.ready()
False

所以看起来像是有效的,但这里是问题:

 >>> result.result 
>>>> (没有返回)
>>> result.get()

当我放入result.get()它只是挂起。我做错了什么?



更新:这是运行日志记录器的前台说,当我启动工作服务器时: p>

 找不到处理程序的记录器多处理

[配置]
- 代理: amqplib:// guest @ localhost:5672 /
- loader:djcelery.loaders.DjangoLoader
- logfile:[stderr] @INFO
- 并发:4
- 事件:OFF
- beat:OFF

[队列]
- 芹菜:交换:芹菜(直接)绑定:芹菜

[任务]
- app.module.add
[2011-07-27 21:17:19,990:WARNING / MainProcess]芹菜@红杉已经开始了。

C:\Python27\lib\site-packages\django-celery-2.2.4-py2.7.egg\djcelery\loaders.py:80:UserWarning:Using settings.DEBUG导致内存泄漏,在生产环境中不要使用此设置!
warnings.warn(使用settings.DEBUG导致内存泄漏,从不

那么当我输入命令:

 >>> result = add(4,4)

这出现在错误日志中:

  [2011-07-28 11:00:39,352:ERROR / MainProcess]未知任务被忽略:任务task.add未注册,请确保已导入。 {'retries':0,'task':'tasks.add','args':(4,4),'expires':None,'ta':None 
'kwargs':{}, 'id':'225ec0ad-195e-438b-8905-ce28e7b6ad9'}
追溯(最近的最后一次调用):
文件C:\Python27\..\celery\worker \consumer.py,第368行,在receive_message
Eventer = self.event_dispatcher)
文件C:\Python27\..\celery\worker\job.py,第306行,in_message
** kw)
文件C:\Python27\\ \\\celery\worker\job.py,第275行,__init__
self.task = tasks [self.task_name]
文件C:\Python27\ .. .\celery\registry.py,第59行,__getitem__
提升self.NotRegistered(key)
NotRegistered:'tasks.add'
/ pre>

如何让这个任务得到正确的注册和处理?谢谢。



更新2:



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



退出了manage.py shell并输入一个python shell并输入以下内容:

 >>>来自app.module.tasks import add 
>>> result = add.delay(4,4)
>>> result.ready()
False
>>> result.result
>>>> (没有返回)
>>> result.get()
(它只是挂在那里)

所以我得到了行为,但新的日志消息。从日志中,服务器正在工作,但不会将结果反馈给:

  [2011-07- 28 11:39:21,706:INFO / MainProcess]从代理获取任务:app.module.tasks.add [7e794740-63c4-42fb-acd5-b9c6fcd545c3] 
[2011-07-28 11:39: 21,706:INFO / MainProcess]任务app.module.tasks.add [7e794740-63c4-42fb-acd5-b9c6fcd545c3]成功在0.04600000038147s:8

所以服务器得到了这个任务,它计算出了正确的答案,但它不会发回来?为什么不这样?

解决方案

我从另一个stackoverflow文章中找到了我的问题的解决方案:为什么Celery在Python shell中工作,但不是在我的Django视图? (导入问题)



我不得不将这些行添加到我的设置文件中:

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

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

  @task(name =module.tasks.add)

服务器和客户端必须被通知任务名。芹菜和django-celery教程在他们的教程中省略了这些。


UDATE3: found the issue. See the answer below.

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?

Original Post:

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

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

I then tried to go through the djcelery tutorial.

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',

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

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

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:\Python27\lib\site-packages\django-celery-2.2.4-py2.7.egg\djcelery\loaders.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"

then when I put in the command:

>>> result = add(4,4)

This appears in the error log:

[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\..\celery\worker\consumer.py",line 368, in receive_message
      Eventer=self.event_dispatcher)
   File "C:\Python27\..\celery\worker\job.py",line 306, in from_message 
       **kw)
   File "C:\Python27\..\celery\worker\job.py",line 275, in __init__
       self.task = tasks[self.task_name]
   File "C:\Python27\...\celery\registry.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.

UPDATE 2:

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

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?

解决方案

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", )

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

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

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教程不返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆