在我的Django应用程序中运行celery beat worker

时间:2019-05-13 09:22:35

标签: django django-celery

我将celery用于我的Django项目,并且要运行celery beat worker,请使用命令

celery -A <projectname> worker -B

这很好,但是如果我想从我的应用程序中执行此命令,则

我运行此命令

celery -A path\to\projectname worker -B

然后我收到此错误ImportError:不支持按文件名导入。

这是我的目录结构。

project
|-- 1
|-- build.csv
|-- build_info.csv
|-- celerybeat-schedule
|-- db.sqlite3
|-- application
|   |-- !
|   |-- !!
|   |-- 1
|   |-- admin.py
|   |-- admin.pyc
|   |-- celery_task1.log
|   |-- celery_tasks.logs
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- models.py
|   |-- models.pyc
|   |-- tasks.py
|   |-- tasks.pyc
|   |-- templates
|   |   |-- admin
|   |   |   `-- application
|   |   |       `-- download_csv_report
|   |   |           `-- change_form.html
|   |   `-- application
|   |       |-- index.html
|   |       |-- jenkinschart.html
|   |       `-- jenkins.html
|   |-- tests.py
|   |-- urls.py
|   |-- urls.pyc
|   |-- views.py
|   `-- views.pyc
|-- project
|   |-- celery.py
|   |-- celery.pyc
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- settings.py
|   |-- settings.pyc
|   |-- urls.py
|   |-- urls.pyc
|   |-- wsgi.py
|   `-- wsgi.pyc
|-- manage.py
|-- start.py
|-- static

这是我的错误堆栈跟踪

Traceback (most recent call last):
  File "/home/shopclues/project/env/bin/celery", line 11, in <module>
    sys.exit(main())
  File "/home/shopclues/project/env/local/lib/python2.7/site-packages/celery/__main__.py", line 14, in main
    _main()
  File "/home/shopclues/project/env/local/lib/python2.7/site-packages/celery/bin/celery.py", line 326, in main
    cmd.execute_from_commandline(argv)
  File "/home/shopclues/project/env/local/lib/python2.7/site-packages/celery/bin/celery.py", line 488, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/home/shopclues/project/env/local/lib/python2.7/site-packages/celery/bin/base.py", line 279, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "/home/shopclues/project/env/local/lib/python2.7/site-packages/celery/bin/base.py", line 481, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "/home/shopclues/project/env/local/lib/python2.7/site-packages/celery/bin/base.py", line 503, in find_app
    return find_app(app, symbol_by_name=self.symbol_by_name)
  File "/home/shopclues/project/env/local/lib/python2.7/site-packages/celery/app/utils.py", line 355, in find_app
    sym = symbol_by_name(app, imp=imp)
  File "/home/shopclues/project/env/local/lib/python2.7/site-packages/celery/bin/base.py", line 506, in symbol_by_name
    return imports.symbol_by_name(name, imp=imp)
  File "/home/shopclues/project/env/local/lib/python2.7/site-packages/kombu/utils/imports.py", line 56, in symbol_by_name
    module = imp(module_name, package=package, **kwargs)
  File "/home/shopclues/project/env/local/lib/python2.7/site-packages/celery/utils/imports.py", line 101, in import_from_cwd
    return imp(module, package=package)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)

有什么方法可以从我的应用程序中运行celery beat worker命令。希望能得到任何帮助

1 个答案:

答案 0 :(得分:-1)

如果您位于Django应用程序的根目录中,并且已将celery应用程序添加到django设置中。您的celery.py文件应如下所示。

from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
from my_django_app import settings as app_settings

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_django_app.settings')
APP = Celery('my_django_app', broker=app_settings.BROKER_URL)

# Using a string here means the worker will not have to
# pickle the object when using Windows. 
APP.config_from_object('django.conf:settings')
APP.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

然后,您可以在Django根目录中以这样的方式运行命令

celery -A my_django_app worker -B