我在这里经历了这个例子:
http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
我所有的任务都在名为 tasks.py 的文件中。
更新 celery 并从示例 django 添加文件后,无论我尝试什么,都会引发以下错误:
ImportError: cannot import name Celery
问题可能是由以下原因引起的吗?
app.autodiscover_tasks(settings.INSTALLED_APPS, related_name='tasks')
因为它遍历所有具有以下导入的 tasks.py 文件。
from cloud.celery import app
云/芹菜.py:
from __future__ import absolute_import
import os, sys
from celery import Celery
from celery.schedules import crontab
from django.conf import settings
BROKER_URL = 'redis://:PASSWORD@localhost'
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cloud.settings')
app = Celery('cloud', broker=BROKER_URL)
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(settings.INSTALLED_APPS, related_name='tasks')
if "test" in sys.argv:
app.conf.update(
CELERY_ALWAYS_EAGER=True,
)
print >> sys.stderr, 'CELERY_ALWAYS_EAGER = True'
CELERYBEAT_SCHEDULE = {
'test_rabbit_running': {
"task": "retail.tasks.test_rabbit_running",
"schedule": 3600, #every hour
},
[..]
app.conf.update(
CELERYBEAT_SCHEDULE=CELERYBEAT_SCHEDULE
)
零售/tasks.py:
from cloud.celery import app
import logging
from celery.utils.log import get_task_logger
logger = get_task_logger('tasks')
logger.setLevel(logging.DEBUG)
@app.task
def test_rabbit_running():
import datetime
utcnow = datetime.datetime.now()
logger.info('CELERY RUNNING')
当我尝试访问无效的 url(如 /foobar)时,会发生错误。
这是完整的回溯:
Traceback (most recent call last):
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 126, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 255, in __call__
response = self.get_response(request)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 178, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 220, in handle_uncaught_exception
if resolver.urlconf_module is None:
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 342, in urlconf_module
self._urlconf_module = import_module(self.urlconf_name)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/opt/src/slicephone/cloud/cloud/urls.py", line 52, in
urlpatterns += patterns('', url(r'^search/', include('search.urls')))
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 25, in include
urlconf_module = import_module(urlconf_module)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/opt/src/slicephone/cloud/search/urls.py", line 5, in
from handlers import SearchHandler
File "/opt/src/slicephone/cloud/search/handlers.py", line 15, in
from places import handlers as placeshandler
File "/opt/src/slicephone/cloud/places/handlers.py", line 23, in
import api as placesapi
File "/opt/src/slicephone/cloud/places/api.py", line 9, in
from djapi import *
File "/opt/src/slicephone/cloud/places/djapi.py", line 26, in
from tasks import add_single_place, add_multiple_places
File "/opt/src/slicephone/cloud/places/tasks.py", line 2, in
from cloud.celery import app
File "/opt/src/slicephone/cloud/cloud/celery.py", line 4, in
from celery import Celery
File "/opt/src/slicephone/cloud/cloud/celery.py", line 4, in
from celery import Celery
ImportError: cannot import name Celery