i'm trying setup periodic-tasks via python-celery. following project structure:
proj/ proj/__init__.py proj/celeryconfig.py proj/celery.py proj/tasks.py
celery.py
from __future__ import absolute_import celery import celery # instantiate celery object app = celery( include=['proj.tasks']) # optional configuration, see 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 celery daemon celery.schedules import crontab # default rabbitmq broker broker_url = 'amqp://' # default rabbitmq backend celery_result_backend = 'amqp://' # timezone, should 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 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 try start celery worker command
celery -a proj worker -b
i 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 remove code required periodic task celeryconfig.py, i.e following part:-
celerybeat_schedule = { 'every-minute': { 'task': 'proj.tasks.add', 'schedule': crontab(minute='*/1'), 'args': (1,2), }, }
kindly help!
the problem should have imported absolute_import __future__
in celeryconfig.py
doing solved problem.
from __future__ import absolute_import
Comments
Post a Comment