RuntimeError:'list'必须是None或列表,而不是<class'str'=“”>尝试启动芹菜工作者

时间:2016-03-07 08:12:44

标签: python django celery

我正在尝试在跟随First Steps With Django时添加芹菜任务,但我收到以下错误:

Traceback (most recent call last):
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/bin/celery", line 11, in <module>
    sys.exit(main())
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/celery/__main__.py", line 30, in main
    main()
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/celery/bin/celery.py", line 81, in main
    cmd.execute_from_commandline(argv)
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/celery/bin/celery.py", line 770, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/celery/bin/base.py", line 311, in execute_from_commandline
    return self.handle_argv(self.prog_name, argv[1:])
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/celery/bin/celery.py", line 762, in handle_argv
    return self.execute(command, argv)
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/celery/bin/celery.py", line 694, in execute
    ).run_from_argv(self.prog_name, argv[1:], command=argv[0])
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/celery/bin/worker.py", line 179, in run_from_argv
    return self(*args, **options)
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/celery/bin/base.py", line 274, in __call__
    ret = self.run(*args, **kwargs)
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/celery/bin/worker.py", line 212, in run
    state_db=self.node_format(state_db, hostname), **kwargs
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/celery/worker/__init__.py", line 95, in __init__
    self.app.loader.init_worker()
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/celery/loaders/base.py", line 128, in init_worker
    self.import_default_modules()
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/celery/loaders/base.py", line 116, in import_default_modules
    signals.import_modules.send(sender=self.app)
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/celery/utils/dispatch/signal.py", line 166, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/amqp/utils.py", line 42, in __call__
    self.set_error_state(exc)
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/amqp/utils.py", line 39, in __call__
    **dict(self.kwargs, **kwargs) if self.kwargs else kwargs
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/celery/app/base.py", line 330, in _autodiscover_tasks
    self.loader.autodiscover_tasks(packages, related_name)
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/celery/loaders/base.py", line 252, in autodiscover_tasks
    related_name) if mod)
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/celery/loaders/base.py", line 273, in autodiscover_tasks
    return [find_related_module(pkg, related_name) for pkg in packages]
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/celery/loaders/base.py", line 273, in <listcomp>
    return [find_related_module(pkg, related_name) for pkg in packages]
  File "/Users/amrullahzunzunia/virtualenvs/flyrobe_new/lib/python3.5/site-packages/celery/loaders/base.py", line 295, in find_related_module
    _imp.find_module(related_name, pkg_path)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/imp.py", line 270, in find_module
    "not {}".format(type(name)))
RuntimeError: 'list' must be None or a list, not <class 'str'>

这是我的项目结构:

  • 项目
    • 配置
      • 设置
        • base.py
        • local.py
        • production.py
      • celery.py#有一个芹菜应用程序
      • urls.py
      • wsgi.py
      • models.py
      • views.py
      • tasks.py#有一个芹菜任务

这是我的config / celery.py:

from __future__ import absolute_import
import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.local')

from django.conf import settings

app = Celery('config')

app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

这是我的config / settings / base.py:

THIRD_PARTY_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.postgres',
    'django.contrib.gis',
    'oauth2_provider',
    'rest_framework',
    'rest_framework_gis',
    'import_export',
    'braces',
    'social.apps.django_app.default',
    'rest_framework_social_oauth2',
]

CUSTOM_APPS = [
    'miscellaneous',
    # more apps
]

INSTALLED_APPS = THIRD_PARTY_APPS + CUSTOM_APPS

BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Kolkata'

这是我的config / settings / local.py:

from .base import *
LOCAL_APPS = [
    'debug_toolbar',
]
INSTALLED_APPS.extend(LOCAL_APPS)

这是我的杂项/ tasks.py:

from celery import shared_task

@shared_task
def log_request_meta(request_meta):
    # this is not complete yet
    return {"a": "b"}

我正在使用python-3.5和django 1.9。我无法弄清楚我错过了什么或我错在哪里,因为我完全按照上面提到的教程进行了操作。

更新 芹菜版本为3.1.20

3 个答案:

答案 0 :(得分:35)

这就是我解决问题的方法:

我看到我的某个应用丢失了__init__.py,这导致了app.autodiscover_tasks(settings.INSTALLED_APPS)的问题

我添加了缺少的__init__.py和芹菜工人,没有任何问题

答案 1 :(得分:4)

当Celery autodiscover_tasks出于某种原因无法加载其中一个INSTALLED_APPS时,会引发此错误。在我的情况下,它是一个带有tasks.py文件的目录,但由于Git提交不完整而缺少其他应用程序文件(models.py等)。

答案 2 :(得分:1)

第一个问题 - 我认为这条线实际应该是

app.autodiscover_tasks(settings.INSTALLED_APPS)

你为什么使用lambda?

第二个回溯 - 在miscellaneous/middleware.py中你正在调用一个传递wsgiref.util.FileWrapper对象的芹菜任务 - 但是你使用的是无法序列化对象实例的JSON序列化程序 - 你需要使用相反,泡菜序列化器。

请参阅docs

中的相关部分