Celery django:启动工作程序-ImportError:没有名为XXX的模块

时间:2018-10-10 13:03:07

标签: python django celery

我很难为芹菜工人服务。

exemple here可以正常工作,并且不受文档限制。

我的问题是我的实际配置文件夹结构如下:

djangosite/
 |-- package/
 |    |-- __init__.py
 |    |-- laboratory/
 |    |    |-- __init__.py
 |    |    |-- migrations/
 |    |    |-- templates/
 |    |    |-- apps.py
 |    |    |-- models.py
 |    |    +-- views.py
 |-- mysite/
 |    |-- __init__.py
 |    |-- settings.py
 |    |-- urls.py
 |    |-- celery.py
 |    +-- wsgi.py
 |-- manage.py

当示例在文件夹中显示应用程序时,我的应用程序是程序包的子文件夹。 __ init __ mysite文件夹:

from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app

__all__ = ['celery_app']

celery.py 包含:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

app = Celery('celery_app')
# This reads, e.g., CELERY_ACCEPT_CONTENT = ['json'] from settings.py:
app.config_from_object('django.conf:settings')

# For autodiscover_tasks to work, you must define your tasks in a file called 'tasks.py'.
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


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

我启动: celery worker -l info -A mysite

芹菜找不到laboratory模块:

Traceback (most recent call last):
  File "C:\Python27\Lib\runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "C:\Python27\Lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "D:\Developpements\VirtualEnv\laboProd\Scripts\celery.exe\__main__.py", line 9, in <module>
  File "d:\developpements\virtualenv\laboprod\lib\site-packages\celery\__main__.py", line 30, in main
    main()
  File "d:\developpements\virtualenv\laboprod\lib\site-packages\celery\bin\celery.py", line 81, in main
    cmd.execute_from_commandline(argv)
  File "d:\developpements\virtualenv\laboprod\lib\site-packages\celery\bin\celery.py", line 793, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "d:\developpements\virtualenv\laboprod\lib\site-packages\celery\bin\base.py", line 311, in execute_from_commandline
    return self.handle_argv(self.prog_name, argv[1:])
  File "d:\developpements\virtualenv\laboprod\lib\site-packages\celery\bin\celery.py", line 785, in handle_argv
    return self.execute(command, argv)
  File "d:\developpements\virtualenv\laboprod\lib\site-packages\celery\bin\celery.py", line 717, in execute
    ).run_from_argv(self.prog_name, argv[1:], command=argv[0])
  File "d:\developpements\virtualenv\laboprod\lib\site-packages\celery\bin\worker.py", line 179, in run_from_argv
    return self(*args, **options)
  File "d:\developpements\virtualenv\laboprod\lib\site-packages\celery\bin\base.py", line 274, in __call__
    ret = self.run(*args, **kwargs)
  File "d:\developpements\virtualenv\laboprod\lib\site-packages\celery\bin\worker.py", line 212, in run
    state_db=self.node_format(state_db, hostname), **kwargs
  File "d:\developpements\virtualenv\laboprod\lib\site-packages\celery\worker\__init__.py", line 95, in __init__
    self.app.loader.init_worker()
  File "d:\developpements\virtualenv\laboprod\lib\site-packages\celery\loaders\base.py", line 128, in init_worker
    self.import_default_modules()
  File "d:\developpements\virtualenv\laboprod\lib\site-packages\celery\loaders\base.py", line 116, in import_default_modules
    signals.import_modules.send(sender=self.app)
  File "d:\developpements\virtualenv\laboprod\lib\site-packages\celery\utils\dispatch\signal.py", line 166, in send
    response = receiver(signal=self, sender=sender, **named)
  File "d:\developpements\virtualenv\laboprod\lib\site-packages\celery\fixups\django.py", line 73, in on_import_modules
    self.worker_fixup.validate_models()
  File "d:\developpements\virtualenv\laboprod\lib\site-packages\celery\fixups\django.py", line 158, in validate_models
    django_setup()
  File "d:\developpements\virtualenv\laboprod\lib\site-packages\django\__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "d:\developpements\virtualenv\laboprod\lib\site-packages\django\apps\registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "d:\developpements\virtualenv\laboprod\lib\site-packages\django\apps\config.py", line 94, in create
    module = import_module(entry)
  File "C:\Python27\Lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named laboratory

该如何解决?我需要将我的应用程序放置在Django根文件夹中吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下代码启动芹菜:Celery -A YOURPROJECTNAME worker -l INFO来进行节拍时间表:Celery -A YOURPROJECTNAME worker -b -l INFO