用Celery设置Django

时间:2018-02-22 13:36:49

标签: python django celery

我正在尝试在我的开发服务器上使用Django设置芹菜,在Windows上运行。以下是我所做的改变。我按照此链接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']

celery.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:\Users\jonas>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:\users\jonas\envs\noisebox-env\lib\site-packages\celery\worker\consumer\consumer.py", line 561, in on_task_received
    strategy = strategies[type_]
KeyError: 'rehearsalbooking.tasks.calculate_timeslots'

当我运行命令检查工作人员的noisebox

celery -A noisebox worker -l info

Traceback (most recent call last):
  File "c:\users\jonas\appdata\local\programs\python\python36-32\Lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\jonas\appdata\local\programs\python\python36-32\Lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\jonas\Envs\noisebox-env\Scripts\celery.exe\__main__.py", line 9, in <module>
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\celery\__main__.py", line 14, in main
    _main()
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\celery\bin\celery.py", line 326, in main
    cmd.execute_from_commandline(argv)
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\celery\bin\celery.py", line 488, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\celery\bin\base.py", line 279, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\celery\bin\base.py", line 481, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "c:\users\jonas\envs\noisebox-env\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\jonas\envs\noisebox-env\lib\site-packages\celery\app\utils.py", line 355, in find_app
    sym = symbol_by_name(app, imp=imp)
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\celery\bin\base.py", line 506, in symbol_by_name
    return imports.symbol_by_name(name, imp=imp)
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\kombu\utils\imports.py", line 56, in symbol_by_name
    module = imp(module_name, package=package, **kwargs)
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\celery\utils\imports.py", line 101, in import_from_cwd
    return imp(module, package=package)
  File "c:\users\jonas\envs\noisebox-env\lib\importlib\__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'

它告诉我没有安装应用程序。对此设置中可能出现的问题的任何想法?

1 个答案:

答案 0 :(得分:0)

当Django启动时,可能存在未被捕获的错误。例如,如果芹菜没有对日志文件的正确权限。

如果您在celery.py文件中运行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))