I'm trying to get Celery working with django to setup scheduled tasks. I've tried looking over the first steps w/ Celery and the first steps w/ Django tutorials and neither has been working for me. Here's my project layout with relevant files:
Python 3.5.1
Django 1.10
Celery 4.0.2
RabbitMQ 3.6.6
OTP 19.2
mysite/ (project name)
polls/ (myapp)
tasks
...
mysite/
__init__
celery
settings
...
manage
...
mysite/__init__.py:
from __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ['celery_app']
polls/celery.py:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
app = Celery('mysite', backend='amqp', broker='amqp://guest@localhost//',include=['polls.tasks'])
# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
polls/tasks.py:
# Create your tasks here
from __future__ import absolute_import, unicode_literals
from celery import shared_task
@shared_task
def add(x, y):
return x + y
@shared_task
def mul(x, y):
return x * y
@shared_task
def xsum(numbers):
return sum(numbers)
I tried to run:
\mysite> celery -A tasks worker --loglevel=info
And the result was:
Traceback (most recent call last): File "c:\users\username\appdata\local\programs\python\python35\lib\runpy.py", line 170, in _run_module_as_main
"__main__", mod_spec)
File "c:\users\username\appdata\local\programs\python\python35\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\username\AppData\Local\Programs\Python\Python35\Scripts\celery.exe\__main__.py", line 9, in <module>
File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\celery\__main__.py", line 14, in main
_main()
File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\celery\bin\celery.py", line 326, in main
cmd.execute_from_commandline(argv)
File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\celery\bin\celery.py", line 488, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\celery\bin\base.py", line 279, in execute_from_commandline
argv = self.setup_app_from_commandline(argv)
File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\celery\bin\base.py", line 481, in setup_app_from_commandline
self.app = self.find_app(app)
File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\celery\bin\base.py", line 503, in find_app
return find_app(app, symbol_by_name=self.symbol_by_name)
File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\celery\app\utils.py", line 355, in find_app
sym = symbol_by_name(app, imp=imp)
File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\celery\bin\base.py", line 506, in symbol_by_name
return imports.symbol_by_name(name, imp=imp)
File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\kombu\utils\imports.py", line 56, in symbol_by_name
module = imp(module_name, package=package, **kwargs)
File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\celery\utils\imports.py", line 101, in import_from_cwd
return imp(module, package=package)
File "c:\users\username\appdata\local\programs\python\python35\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'tasks'
The problem is in this line:
\mysite> celery -A tasks worker --loglevel=info
There is no tasks.py file in the outer mysite folder:
I think what you need is
\mysite> celery -A polls/tasks worker --loglevel=info
Or :
\mysite> celery worker --app=mysite --loglevel=info
\mysite> celery worker -A proj -loglevel=info
The location at which you are executing this command is wrong.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With