为什么 Celery 可以在 Python shell 中工作,但不能在我的 Django 视图中工作? (导入问题)

发布于 2024-10-11 07:39:24 字数 2818 浏览 3 评论 0原文

我安装了 Celery(最新稳定版本。) 我有一个名为 /home/myuser/fable/jobs 的目录。在这个目录中,我有一个名为tasks.py的文件:

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

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

在这个目录中,我还有一个名为celeryconfig.py的文件:

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

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

在我的/etc/profile中,我将这些设置为我的PYTHONPATH:

  • < code>PYTHONPATH=/home/myuser/fable:/home/myuser/fable/jobs

所以我使用控制台运行我的 Celery Worker ($ celeryd --loglevel=INFO),并且我尝试一下。 我打开 Python 控制台并导入任务。然后,我运行提交者。

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

一切正常,正如您在我的控制台中看到的那样,

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

但是当我进入 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."

这很奇怪,因为 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

有人可以帮忙吗?

I installed Celery (latest stable version.)
I have a directory called /home/myuser/fable/jobs. Inside this directory, I have a file called 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:

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:

  • 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.
I open the Python console and import the tasks. 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:

[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.

[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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

匿名的好友 2024-10-18 07:39:24

这就是我所做的,最终

在 Settings.py 中工作,我

CELERY_IMPORTS = ("myapp.jobs", )

在 myapp 文件夹下添加了一个名为 jobs.py 的文件

from celery.decorators import task

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

,然后从命令行运行: python manage.py celeryd -l info

在另一个 shell 中我运行了 python manage.py shell,然后

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

我得到:

16

重要的是,当您添加新函数时,您必须重新运行两个命令 shell。您必须在客户端和服务器上注册该名称。

:-)

This is what I did which finally worked

in Settings.py I added

CELERY_IMPORTS = ("myapp.jobs", )

under myapp folder I created a file called 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

in another shell i ran python manage.py shell, then

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

and the i get:

16

The important point is that you have to rerun both command shells when you add a new function. You have to register the name both on the client and and on the server.

:-)

记忆之渊 2024-10-18 07:39:24

我相信您的tasks.py 文件需要位于django 应用程序中(在settings.py 中注册)才能导入。或者,您可以尝试从主项目或某个应用程序中的 __init__.py 文件导入任务。

还可以尝试从manage.py启动celeryd:(

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

-E-B可能是必要的,也可能不是必要的,但这就是我使用的)。

I believe your tasks.py file needs to be in a django app (that's registered in settings.py) in order to be imported. Alternatively, you might try importing the tasks from an __init__.py file in your main project or one of the apps.

Also try starting celeryd from manage.py:

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

(-E and -B may or may not be necessary, but that's what I use).

羞稚 2024-10-18 07:39:24

请参阅文档中的自动命名和相对导入:

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

任务名称为“tasks.Submitter”(如 celeryd 输出中所列),
但是您将任务导入为“fable.jobs.tasks.Submitter”

我想这里最好的解决方案是工作人员也将其视为“fable.jobs.tasks.Submitter”,
从应用程序的角度来看,它更有意义。

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

See Automatic Naming and Relative Imports, in the docs:

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"

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.

CELERY_IMPORTS = ("fable.jobs.tasks", )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文