简体   繁体   English

为什么Celery在Python shell中工作,但在我的Django视图中却没有? (进口问题)

[英]Why does Celery work in Python shell, but not in my Django views? (import problem)

I installed Celery (latest stable version.) I have a directory called /home/myuser/fable/jobs . 我安装了Celery(最新的稳定版本。)我有一个名为/home/myuser/fable/jobs Inside this directory, I have a file called tasks.py: 在这个目录中,我有一个名为tasks.py的文件:

from celery.decorators import task
from celery.task import Task

class Submitter(Task):
    def run(self, post, **kwargs):
        return "Yes, it works!!!!!!"

Inside this directory, I also have a file called celeryconfig.py: 在这个目录中,我还有一个名为celeryconfig.py的文件:

BROKER_HOST = "localhost"
BROKER_PORT = 5672
BROKER_USER = "abc"
BROKER_PASSWORD = "xyz"
BROKER_VHOST = "fablemq"

CELERY_RESULT_BACKEND = "amqp"
CELERY_IMPORTS = ("tasks", )

In my /etc/profile , I have these set as my PYTHONPATH: 在我的/etc/profile ,我将这些设置为我的PYTHONPATH:

  • PYTHONPATH=/home/myuser/fable:/home/myuser/fable/jobs

So I run my Celery worker using the console ( $ celeryd --loglevel=INFO ), and I try it out. 所以我使用控制台运行我的Celery工作者( $ celeryd --loglevel=INFO ),然后我试试看。 I open the Python console and import the tasks. 我打开Python控制台并导入任务。 Then, I run the Submitter. 然后,我运行提交者。

>>> import fable.jobs.tasks as tasks
>>> s = tasks.Submitter()
>>> s.delay("abc")
<AsyncResult: d70d9732-fb07-4cca-82be-d7912124a987>

Everything works, as you can see in my console 一切正常,正如您在我的控制台中看到的那样

[2011-01-09 17:30:05,766: INFO/MainProcess] Task tasks.Submitter[d70d9732-fb07-4cca-82be-d7912124a987] succeeded in 0.0398268699646s:

But when I go into my Django's views.py and run the exact 3 lines of code as above, I get this: 但是当我进入我的Django的views.py并运行上面的3行代码时,我得到了这个:

[2011-01-09 17:25:20,298: ERROR/MainProcess] Unknown task ignored: "Task of kind 'fable.jobs.tasks.Submitter' is not registered, please make sure it's imported.": {'retries': 0, 'task': 'fable.jobs.tasks.Submitter', 'args': ('abc',), 'expires': None, 'eta': None, 'kwargs': {}, 'id': 'eb5c65b4-f352-45c6-96f1-05d3a5329d53'}
Traceback (most recent call last):
  File "/home/myuser/mysite-env/lib/python2.6/site-packages/celery/worker/listener.py", line 321, in receive_message
    eventer=self.event_dispatcher)
  File "/home/myuser/mysite-env/lib/python2.6/site-packages/celery/worker/job.py", line 299, in from_message
    eta=eta, expires=expires)
  File "/home/myuser/mysite-env/lib/python2.6/site-packages/celery/worker/job.py", line 243, in __init__
    self.task = tasks[self.task_name]
  File "/home/myuser/mysite-env/lib/python2.6/site-packages/celery/registry.py", line 63, in __getitem__
    raise self.NotRegistered(str(exc))
NotRegistered: "Task of kind 'fable.jobs.tasks.Submitter' is not registered, please make sure it's imported."

It's weird, because the celeryd client does show that it's registered, when I launch it. 这很奇怪,因为当我启动它时,celeryd客户端确实显示它已注册。

[2011-01-09 17:38:27,446: WARNING/MainProcess]  
Configuration ->
    . broker -> amqp://GOGOme@localhost:5672/fablemq
    . queues ->
        . celery -> exchange:celery (direct) binding:celery
    . concurrency -> 1
    . loader -> celery.loaders.default.Loader
    . logfile -> [stderr]@INFO
    . events -> OFF
    . beat -> OFF
    . tasks ->
        . tasks.Decayer
        . tasks.Submitter

Can someone help? 有人可以帮忙吗?

This is what I did which finally worked 这就是我最终的工作

in Settings.py I added 在Settings.py中我添加了

CELERY_IMPORTS = ("myapp.jobs", )

under myapp folder I created a file called jobs.py 在myapp文件夹下,我创建了一个名为jobs.py的文件

from celery.decorators import task

@task(name="jobs.add")
def add(x, y):
    return x * y

Then ran from commandline: python manage.py celeryd -l info 然后从命令行运行:python manage.py celeryd -l info

in another shell i ran python manage.py shell, then 在另一个shell中我运行了python manage.py shell,然后

>>> from myapp.jobs import add
>>> result = add.delay(4, 4)
>>> result.result

and the i get: 而我得到:

16 16

The important point is that you have to rerun both command shells when you add a new function. 重要的是,在添加新函数时必须重新运行两个命令shell。 You have to register the name both on the client and and on the server. 您必须在客户端和服务器上注册该名称。

:-) :-)

I believe your tasks.py file needs to be in a django app (that's registered in settings.py) in order to be imported. 我相信你的tasks.py文件需要在django应用程序(在settings.py中注册)才能导入。 Alternatively, you might try importing the tasks from an __init__.py file in your main project or one of the apps. 或者,您可以尝试从主项目或其中一个应用程序中的__init__.py文件中导入任务。

Also try starting celeryd from manage.py: 还可以尝试从manage.py启动celeryd:

$ python manage.py celeryd -E -B -lDEBUG

( -E and -B may or may not be necessary, but that's what I use). -E-B可能是也可能不是必需的,但这就是我使用的)。

See Automatic Naming and Relative Imports, in the docs: 请参阅文档中的自动命名和相对导入:

http://celeryq.org/docs/userguide/tasks.html#automatic-naming-and-relative-imports http://celeryq.org/docs/userguide/tasks.html#automatic-naming-and-relative-imports

The tasks name is "tasks.Submitter" (as listed in the celeryd output), but you import the task as "fable.jobs.tasks.Submitter" 任务名称是“tasks.Submitter”(如celeryd输出中所列),但您将任务导入为“fable.jobs.tasks.Submitter”

I guess the best solution here is if the worker also sees it as "fable.jobs.tasks.Submitter", it makes more sense from an app perspective. 我想这里最好的解决方案是如果工作者也将其视为“fable.jobs.tasks.Submitter”,从应用程序的角度来看它更有意义。

CELERY_IMPORTS = ("fable.jobs.tasks", )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么此python代码挂在import / compile上,但可以在shell中运行? - Why does this python code hang on import/compile but work in the shell? 为什么从 . 导入视图工作和导入视图不? - Why does from . import views work and import views does not? 如何在 Django 视图中导入 celery 任务 - how to import celery tasks in django views 为什么我的Selenium代码在Python Shell中可以工作,但不能在文件中工作? - Why does my Selenium code work in the Python Shell but not from a file? Celery get()在python shell中有效,但在Django视图中无效 - Celery get() works in python shell but not in Django view Django,RabbitMQ和Celery - 为什么Celery在开发中更新我的Django代码后会运行旧版本的任务? - Django, RabbitMQ, & Celery - why does Celery run old versions of my tasks after I update my Django code in development? 导入熊猫在终端中不起作用; 在Python Shell中工作 - Import Pandas Does Not Work In Terminal; Works in Python Shell 为什么python绝对导入不起作用? - Why python absolute import does not work? 为什么我的代码可以在python shell中工作,但是当我双击py文件时却不能工作 - Why does my code work in python shell but not when I double click the py file 为什么我的 subprocess.call 只有在 shell = True 时才有效? Python - Why does my subprocess.call work only when shell = True? Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM