Python Celery 没有名为 app.tasks 的模块



我正在尝试从 Celery 运行演示应用程序

项目目录是/usr/local/proj

[root@merit1 proj]# ls -lh
total 16K
-rw-r--r--. 1 root root 363 Dec 23 00:36 celery.py
-rw-r--r--. 1 root root 522 Dec 23 00:36 celery.pyc
-rw-r--r--. 1 root root   0 Dec 23 00:22 __init__.py
-rw-r--r--. 1 root root 114 Dec 23 00:36 __init__.pyc
-rw-r--r--. 1 root root 211 Dec 23 00:37 tasks.py

和 celery.py

from __future__ import absolute_import
from celery import Celery
app = Celery('proj',
             broker='amqp://guest@localhost//',
             backend='amqp://',
             include=['app.tasks'])
# Optional configuration, see the application user guide.
app.conf.update(
    CELERY_TASK_RESULT_EXPIRES=3600,
)
if __name__ == '__main__':
    app.start()

和 tasks.py 包含

from __future__ import absolute_import
from proj.celery import app

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

@app.task
def mul(x, y):
    return x * y

@app.task
def xsum(numbers):
    return sum(numbers)

但是当我从/usr/local 运行芹菜时,我收到此错误

celery worker --app=proj -l info
Traceback (most recent call last):
  File "/usr/bin/celery", line 9, in <module>
    load_entry_point('celery==3.1.7', 'console_scripts', 'celery')()
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/__main__.py", line 30, in main
    main()
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/bin/celery.py", line 80, in main
    cmd.execute_from_commandline(argv)
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/bin/celery.py", line 723, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/bin/base.py", line 303, in execute_from_commandline
    return self.handle_argv(self.prog_name, argv[1:])
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/bin/celery.py", line 715, in handle_argv
    return self.execute(command, argv)
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/bin/celery.py", line 669, in execute
    ).run_from_argv(self.prog_name, argv[1:], command=argv[0])
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/bin/worker.py", line 175, in run_from_argv
    return self(*args, **options)
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/bin/base.py", line 266, in __call__
    ret = self.run(*args, **kwargs)
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/bin/worker.py", line 208, in run
    state_db=self.node_format(state_db, hostname), **kwargs
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/worker/__init__.py", line 95, in __init__
    self.app.loader.init_worker()
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/loaders/base.py", line 128, in init_worker
    self.import_default_modules()
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/loaders/base.py", line 121, in import_default_modules
    tuple(maybe_list(self.app.conf.CELERY_INCLUDE))
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/loaders/base.py", line 103, in import_task_module
    return self.import_from_cwd(module)
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/loaders/base.py", line 112, in import_from_cwd
    package=package,
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/utils/imports.py", line 101, in import_from_cwd
    return imp(module, package=package)
  File "/usr/lib64/python2.6/contextlib.py", line 34, in __exit__
    self.gen.throw(type, value, traceback)
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/utils/imports.py", line 64, in cwd_in_path
    yield cwd
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/utils/imports.py", line 101, in import_from_cwd
    return imp(module, package=package)
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/loaders/base.py", line 106, in import_module
    return importlib.import_module(module, package=package)
  File "/usr/lib/python2.6/site-packages/importlib-1.0.2-py2.6.egg/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named app.tasks

有什么想法吗?

更新:

celery.py 更新至

from __future__ import absolute_import
from celery import Celery
app = Celery('proj',
             broker='amqp://guest@localhost//',
             backend='amqp://',
             include=['proj.tasks'])
# Optional configuration, see the application user guide.
app.conf.update(
    CELERY_TASK_RESULT_EXPIRES=3600,
)
if __name__ == '__main__':
    app.start()

我现在收到此错误

[root@merit1 local]# celery worker --app=proj -l info
Traceback (most recent call last):
  File "/usr/bin/celery", line 9, in <module>
    load_entry_point('celery==3.1.7', 'console_scripts', 'celery')()
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/__main__.py", line 30, in main
    main()
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/bin/celery.py", line 80, in main
    cmd.execute_from_commandline(argv)
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/bin/celery.py", line 723, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/bin/base.py", line 303, in execute_from_commandline
    return self.handle_argv(self.prog_name, argv[1:])
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/bin/celery.py", line 715, in handle_argv
    return self.execute(command, argv)
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/bin/celery.py", line 669, in execute
    ).run_from_argv(self.prog_name, argv[1:], command=argv[0])
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/bin/worker.py", line 175, in run_from_argv
    return self(*args, **options)
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/bin/base.py", line 266, in __call__
    ret = self.run(*args, **kwargs)
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/bin/worker.py", line 208, in run
    state_db=self.node_format(state_db, hostname), **kwargs
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/worker/__init__.py", line 95, in __init__
    self.app.loader.init_worker()
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/loaders/base.py", line 128, in init_worker
    self.import_default_modules()
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/loaders/base.py", line 121, in import_default_modules
    tuple(maybe_list(self.app.conf.CELERY_INCLUDE))
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/loaders/base.py", line 103, in import_task_module
    return self.import_from_cwd(module)
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/loaders/base.py", line 112, in import_from_cwd
    package=package,
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/utils/imports.py", line 101, in import_from_cwd
    return imp(module, package=package)
  File "/usr/lib64/python2.6/contextlib.py", line 34, in __exit__
    self.gen.throw(type, value, traceback)
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/utils/imports.py", line 64, in cwd_in_path
    yield cwd
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/utils/imports.py", line 101, in import_from_cwd
    return imp(module, package=package)
  File "/usr/lib/python2.6/site-packages/celery-3.1.7-py2.6.egg/celery/loaders/base.py", line 106, in import_module
    return importlib.import_module(module, package=package)
  File "/usr/lib/python2.6/site-packages/importlib-1.0.2-py2.6.egg/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/usr/local/proj/tasks.py", line 8, in <module>
    @app.task
TypeError: 'module' object is not callable

将包含更改为:

app = Celery('proj',
         broker='amqp://guest@localhost//',
         backend='amqp://',
         include=['proj.tasks'])

查看芹菜应用文档

include – 每个工作人员应导入的模块列表。

相关内容

  • 没有找到相关文章

最新更新