我正在努力让Celery与django合作设置预定任务。我已经尝试查看the first steps w/ Celery和the first steps w/ Django教程,但两者都没有为我工作。这是我的项目布局及相关文件:
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']
轮询/ 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))
轮询/ 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)
我试图跑:
\mysite> celery -A tasks worker --loglevel=info
结果是:
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'
答案 0 :(得分:12)
问题出在这一行:
\mysite> celery -A tasks worker --loglevel=info
外部mysite文件夹中没有 tasks.py 文件:
我认为你需要的是
\mysite> celery -A polls/tasks worker --loglevel=info
或者:
\mysite> celery worker --app=mysite --loglevel=info
\mysite> celery worker -A proj -loglevel=info
执行此命令的位置错误。