25

我尝试从命令行启动 Celery 工作服务器:

celery -A tasks worker --loglevel=info

tasks.py 中的代码:

import os
os.environ[ 'DJANGO_SETTINGS_MODULE' ] = "proj.settings"

from celery import task

@task()
def add_photos_task( lad_id ):
...

我得到下一个错误:

Traceback (most recent call last):
  File "/usr/local/bin/celery", line 8, in <module>
    load_entry_point('celery==3.0.12', 'console_scripts', 'celery')()
  File "/usr/local/lib/python2.7/site-packages/celery-3.0.12-py2.7.egg/celery/__main__.py", line 14, in main
    main()
  File "/usr/local/lib/python2.7/site-packages/celery-3.0.12-py2.7.egg/celery/bin/celery.py", line 946, in main
    cmd.execute_from_commandline(argv)
  File "/usr/local/lib/python2.7/site-packages/celery-3.0.12-py2.7.egg/celery/bin/celery.py", line 890, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/usr/local/lib/python2.7/site-packages/celery-3.0.12-py2.7.egg/celery/bin/base.py", line 177, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "/usr/local/lib/python2.7/site-packages/celery-3.0.12-py2.7.egg/celery/bin/base.py", line 295, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "/usr/local/lib/python2.7/site-packages/celery-3.0.12-py2.7.egg/celery/bin/base.py", line 313, in find_app
    return sym.celery
AttributeError: 'module' object has no attribute 'celery'

有人知道为什么找不到“芹菜”属性吗?谢谢你的帮助。

操作系统是 Linux Debian 5。

编辑. 可能是线索。谁能解释我对函数的下一条评论(为什么我们必须确保它在当前目录中找到模块)?

# from celery/utils/imports.py
def import_from_cwd(module, imp=None, package=None):
    """Import module, but make sure it finds modules
    located in the current directory.

    Modules located in the current directory has
    precedence over modules located in `sys.path`.
    """
    if imp is None:
        imp = importlib.import_module
    with cwd_in_path():
        return imp(module, package=package)
4

6 回答 6

26

我忘了在 tasks.py 中创建一个 celery 对象:

from celery import Celery
from celery import task  

celery = Celery('tasks', broker='amqp://guest@localhost//') #!

import os

os.environ[ 'DJANGO_SETTINGS_MODULE' ] = "proj.settings"

@task()
def add_photos_task( lad_id ):
...

之后我们就可以正常启动任务了:

celery -A tasks worker --loglevel=info
于 2012-11-26T16:36:41.233 回答
14

对于由于明显不同的原因而收到相同错误消息的任何人,请注意,如果您的初始化文件中的任何导入失败,您的应用程序将引发这个完全模棱两可AttributeError的问题,而不是最初导致它的异常。

于 2017-07-07T19:02:06.953 回答
12

Celery 使用celery文件来存储你的应用程序的配置,你不能只给一个带有任务的 python 文件并启动 celery。您应该定义celery文件(对于 Celery>3.0;以前是celeryconfig.py)..

celeryd --app app.celery -l 信息

这个例子如何使用配置文件启动 celeryapp/celery.py

这是芹菜文件的示例:https ://github.com/Kami/libcloud-sandbox/blob/master/celeryconfig.py

于 2012-11-26T16:01:56.457 回答
3

我的问题是我将celery变量放在主函数中:

if __name__ == '__main__':  # Remove this row
    app = Flask(__name__)
    celery = make_celery(app) 

什么时候应该放在外面。

于 2018-08-21T13:45:09.233 回答
3

当您运行时celery -A tasks worker --loglevel=info,您的 celery 应用程序应该在模块中公开tasks。它不应该包含在函数或if语句中。

如果您make_celery在另一个文件中,您应该将 celery 应用程序导入到您传递给 celery 的文件中。

于 2018-09-26T10:23:44.703 回答
1

尝试启动芹菜:

celeryd --config=my_app.my_config --loglevel=INFO --purge -Q my_queue

我的中有下一个脚本tasks.py

@task(name="my_queue", routing_key="my_queue")
def add_photos_task( lad_id ):

有下一个脚本my_config.py

CELERY_IMPORTS = \
(
    "my_app.tasks",
)
CELERY_ROUTES = \
{
    "my_queue":
    {
        "queue": "my_queue"
    },
}
CELERY_QUEUES = \
{
    "my_queue":
    {
        "exchange": "my_app",
        "exchange_type": "direct",
        "binding_key": "my_queue"
    },
}
celery = Celery(broker='amqp://guest@localhost//')
于 2013-05-24T07:47:01.927 回答