我試圖讓芹菜使用django來設置計劃任務。我試過看過the first steps w/ Celery和the first steps w/ Django教程,但都沒有爲我工作。這裏是我的項目佈局相關文件:ImportError:沒有名爲'tasks'的模塊
的Python 3.5.1
Django的1.10
芹菜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://[email protected]//',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'
在您的polls目錄中添加'__init __。py'。 –
我仍然得到同樣的錯誤,我想把它放在裏面嗎? – user2361174
我認爲你的問題在於這個聲明'celery -A任務工作者--loglevel = info'。沒有什麼叫'任務'。用芹菜工具執行芹菜--app = mysite' –