我正在尝试在Windows上运行的开发服务器上使用Django设置芹菜。以下是我所做的更改。我遵循此链接的步骤http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
filestructure
Noisebox
-Noisebox
--__init__.py
--celery.py
--settings
-rehearsalbooking
--tasks.py
init.py
from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
__all__ = ['celery_app']
芹菜.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'noisebox.settings')
app = Celery('noisebox')
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
tasks.py
from celery import shared_task
from .models import Room
from django.utils import timezone
@shared_task
def calculate_timeslots(room_id):
room = Room.objects.get(id=room_id)
room.create_timeslots_until_license_expires(timezone.now())
return True
运行任务时,任务将发送到芹菜服务器,但未执行。
(noisebox-env) C:Usersjonas>celery worker
-------------- celery@DESKTOP-MH2SD2S v4.1.0 (latentcall)
---- **** -----
--- * *** * -- Windows-10-10.0.16299-SP0 2018-02-22 14:22:38
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: default:0x29efc50 (.default.Loader)
- ** ---------- .> transport: amqp://guest:**@localhost:5672//
- ** ---------- .> results: disabled://
- *** --- * --- .> concurrency: 8 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> celery exchange=celery(direct) key=celery
[2018-02-22 14:22:39,439: ERROR/MainProcess] Received unregistered task of type 'rehearsalbooking.tasks.calculate_timeslots'.
The message has been ignored and discarded.
Did you remember to import the module containing this task?
Or maybe you're using relative imports?
Please see
http://docs.celeryq.org/en/latest/internals/protocol.html
for more information.
The full contents of the message body was:
'[[1], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": null}]' (78b)
Traceback (most recent call last):
File "c:usersjonasenvsnoisebox-envlibsite-packagesceleryworkerconsumerconsumer.py", line 561, in on_task_received
strategy = strategies[type_]
KeyError: 'rehearsalbooking.tasks.calculate_timeslots'
,当我运行命令以检查工人的噪声箱
时芹菜-A噪声盒工作者-L信息
Traceback (most recent call last):
File "c:usersjonasappdatalocalprogramspythonpython36-32Librunpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:usersjonasappdatalocalprogramspythonpython36-32Librunpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:UsersjonasEnvsnoisebox-envScriptscelery.exe__main__.py", line 9, in <module>
File "c:usersjonasenvsnoisebox-envlibsite-packagescelery__main__.py", line 14, in main
_main()
File "c:usersjonasenvsnoisebox-envlibsite-packagescelerybincelery.py", line 326, in main
cmd.execute_from_commandline(argv)
File "c:usersjonasenvsnoisebox-envlibsite-packagescelerybincelery.py", line 488, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "c:usersjonasenvsnoisebox-envlibsite-packagescelerybinbase.py", line 279, in execute_from_commandline
argv = self.setup_app_from_commandline(argv)
File "c:usersjonasenvsnoisebox-envlibsite-packagescelerybinbase.py", line 481, in setup_app_from_commandline
self.app = self.find_app(app)
File "c:usersjonasenvsnoisebox-envlibsite-packagescelerybinbase.py", line 503, in find_app
return find_app(app, symbol_by_name=self.symbol_by_name)
File "c:usersjonasenvsnoisebox-envlibsite-packagesceleryapputils.py", line 355, in find_app
sym = symbol_by_name(app, imp=imp)
File "c:usersjonasenvsnoisebox-envlibsite-packagescelerybinbase.py", line 506, in symbol_by_name
return imports.symbol_by_name(name, imp=imp)
File "c:usersjonasenvsnoisebox-envlibsite-packageskombuutilsimports.py", line 56, in symbol_by_name
module = imp(module_name, package=package, **kwargs)
File "c:usersjonasenvsnoisebox-envlibsite-packagesceleryutilsimports.py", line 101, in import_from_cwd
return imp(module, package=package)
File "c:usersjonasenvsnoisebox-envlibimportlib__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'noisebox'
它告诉我该应用程序未安装。关于此设置中可能出了什么问题的任何想法?
当django启动时,可能会出现错误的错误。例如,如果芹菜对日志文件没有正确的权限。
如果您在芹菜文件中运行 django.setup()
,则可以在开始时看到任何错误。
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'noisebox.settings')
django.setup()
app = Celery('noisebox')
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))