0

我正在尝试通过 Python-Celery 设置周期性任务。以下是我的项目结构:

proj/
proj/__init__.py
proj/celeryconfig.py
proj/celery.py
proj/tasks.py

芹菜.py

from __future__ import absolute_import

from celery import Celery

# instantiate Celery object
app = Celery(
                include=['proj.tasks'])

# Optional configuration, see the application user guide.

# import celery config file
app.config_from_object('proj.celeryconfig')

app.conf.update(
    CELERY_TASK_RESULT_EXPIRES=3600,
)

if __name__ == '__main__':
    app.start()

芹菜配置文件

# config file for Celery Daemon
from celery.schedules import crontab

# default RabbitMQ broker
BROKER_URL = 'amqp://'

# default RabbitMQ backend
CELERY_RESULT_BACKEND = 'amqp://'

# TimeZone, this should be changed
CELERY_TIMEZONE = 'Asia/Kolkata'

CELERYBEAT_SCHEDULE = {
    'every-minute': {
        'task': 'proj.tasks.add',
        'schedule': crontab(minute='*/1'),
        'args': (1,2),
    },
}

任务.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)

当我尝试使用命令启动芹菜工人时

celery -A proj worker -B

我收到以下错误:-

Traceback (most recent call last):
  File "/usr/bin/celery", line 11, in <module>
    sys.exit(main())
  File "/usr/lib/python2.7/site-packages/celery/__main__.py", line 30, in main
    main()
  File "/usr/lib/python2.7/site-packages/celery/bin/celery.py", line 81, in main
    cmd.execute_from_commandline(argv)
  File "/usr/lib/python2.7/site-packages/celery/bin/celery.py", line 769, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/usr/lib/python2.7/site-packages/celery/bin/base.py", line 305, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "/usr/lib/python2.7/site-packages/celery/bin/base.py", line 465, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "/usr/lib/python2.7/site-packages/celery/bin/base.py", line 485, in find_app
    return find_app(app, symbol_by_name=self.symbol_by_name)
  File "/usr/lib/python2.7/site-packages/celery/app/utils.py", line 248, in find_app
    symbol_by_name=symbol_by_name, imp=imp,
  File "/usr/lib/python2.7/site-packages/celery/app/utils.py", line 232, in find_app
    sym = imp(app)
  File "/usr/lib/python2.7/site-packages/celery/utils/imports.py", line 101, in import_from_cwd
    return imp(module, package=package)
  File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/dev/Workspace/Learning/celery_fun/proj/celery.py", line 19, in <module>
    app.conf.update(
  File "/usr/lib/python2.7/site-packages/kombu/utils/__init__.py", line 322, in __get__
    value = obj.__dict__[self.__name__] = self.__get(obj)
  File "/usr/lib/python2.7/site-packages/celery/app/base.py", line 629, in conf
    return self._get_config()
  File "/usr/lib/python2.7/site-packages/celery/app/base.py", line 453, in _get_config
    self.loader.config_from_object(self._config_source)
  File "/usr/lib/python2.7/site-packages/celery/loaders/base.py", line 140, in config_from_object
    obj = self._smart_import(obj, imp=self.import_from_cwd)
  File "/usr/lib/python2.7/site-packages/celery/loaders/base.py", line 161, in _smart_import
    return symbol_by_name(path, imp=imp)
  File "/usr/lib/python2.7/site-packages/kombu/utils/__init__.py", line 97, in symbol_by_name
    return getattr(module, cls_name) if cls_name else module
AttributeError: 'module' object has no attribute 'celeryconfig'

如果我从 celeryconfig.py 中删除 Periodic 任务所需的代码,则 Worker 运行良好,即以下部分:-

CELERYBEAT_SCHEDULE = {
    'every-minute': {
        'task': 'proj.tasks.add',
        'schedule': crontab(minute='*/1'),
        'args': (1,2),
    },
}

请帮忙!

4

1 回答 1

4

问题是我应该absolute_import from __future__ 进口celeryconfig.py

这样做解决了这个问题。

from __future__ import absolute_import
于 2015-05-08T08:43:02.617 回答