I'm trying to setup a periodic-tasks via Python-Celery. Following is my project structure:
我正在尝试通过Python-Celery设置一个周期性任务。以下是我的项目结构:
proj/
proj/__init__.py
proj/celeryconfig.py
proj/celery.py
proj/tasks.py
celery.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()
celeryconfig.py
# 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),
},
}
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)
When I try to start the Celery worker with the command
当我尝试使用命令启动Celery工作程序时
celery -A proj worker -B
I get the following error:-
我收到以下错误: -
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'
The Worker runs fine if I remove the code required for the Periodic task from the celeryconfig.py, i.e the following part:-
如果我从celeryconfig.py中删除了Periodic任务所需的代码,那么Worker运行正常,即以下部分: -
CELERYBEAT_SCHEDULE = {
'every-minute': {
'task': 'proj.tasks.add',
'schedule': crontab(minute='*/1'),
'args': (1,2),
},
}
Kindly help!
1 个解决方案
#1
The problem was that I should have imported absolute_import from __future__
in celeryconfig.py
问题是我应该从celeryconfig.py中的__future__导入absolute_import
Doing this solved the problem.
这样做解决了这个问题。
from __future__ import absolute_import
#1
The problem was that I should have imported absolute_import from __future__
in celeryconfig.py
问题是我应该从celeryconfig.py中的__future__导入absolute_import
Doing this solved the problem.
这样做解决了这个问题。
from __future__ import absolute_import