【问题标题】:Celery 3 worker with a Django Herokuapp带有 Django Herokuapp 的 Celery 3 工人
【发布时间】:2014-08-20 13:36:09
【问题描述】:

我有一个 Django 应用程序,它的项目布局如 django book 的两勺所述。

├── CONTRIBUTORS.txt
├── LICENSE.txt
├── Procfile
├── README.rst
├── docs
├── requirements
│   ├── base.txt
│   ├── local.txt
│   ├── production.txt
│   └── test.txt
├── requirements.txt
└── PROJECT_NAME
    ├── __init__.py
    ├── app_name
    │   ├── __init__.py
    │   ├── models.py
    │   ├── views.py
    ├── manage.py
    ├── PROJECT_NAME_APP
    │   ├── __init__.py
    │   ├── celery.py
    │   ├── settings
    │   │   ├── __init__.py
    │   │   ├── base.py
    │   │   ├── local.py
    │   │   ├── production.py
    │   │   ├── test.py
    │   ├── urls.py
    │   ├── wsgi.py
    ├── static
    │   ├── css
    │   ├── fonts
    │   └── js
    └── templates

将其部署到 HerokuHeroku 文档略有不同,因为应用程序的文件路径略有不同。但是,我将它们修改为适当的路径,并且一切正常。请注意,我的 PROJECT_NAME_APP 文件夹实际上与我的顶级项目文件夹的名称不同,这是我为了让应用程序在 Heroku 上运行而进行的更改

我现在正在尝试添加一个Celery 工作人员。我正在使用不需要django-celery 包的Celery 版本。在我的 Procfile 我有这一行来定义工人:

工人:芹菜工人--app=project_name.project_name_app.celery

我创建的 celery 文件位于celery docs 之后,位于 PROJECT_NAME_APP 中。

import os

from __future__ import absolute_import
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name_app.settings.local')
app = Celery('project_name_app')
app.config_from_object('django.conf:settings')

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

我遇到了一些关于在 Heroku 中设置 DJANGO_SETTINGS_MODULE 的建议,但我这样做了,但这似乎破坏了其他东西,所以我取消了它。

我现在遇到的错误是:

ImportError: Could not import settings 'project_name_app.settings.local' (Is it on sys.path?): No module named project_name_app.settings.local

我理解错误的含义,但我发现很难在 Heroku 上进行调试。我尝试在本地使用foreman start 来调试我的Procfile,但由于本地与生产设置,它再次引发与我的SECRET_KEY 有关的错误。

谁能看到为什么我的设置在生产中运行时抛出错误?

【问题讨论】:

    标签: python django heroku celery


    【解决方案1】:

    因此,我在wsgi.py 文件中注意到的设置玩了几个小时

    import os
    from os.path import abspath, dirname
    from sys import path
    
    SITE_ROOT = dirname(dirname(abspath(__file__)))
    path.append(SITE_ROOT)
    
    # We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
    # if running multiple sites in the same mod_wsgi process. To fix this, use
    # mod_wsgi daemon mode with each site in its own daemon process, or use
    # os.environ["DJANGO_SETTINGS_MODULE"] = "jajaja.settings"
    
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "restaurants_app.settings.production")
    
    # This application object is used by any WSGI server configured to use this
    # file. This includes Django's development server, if the WSGI_APPLICATION
    # setting points here.
    from django.core.wsgi import get_wsgi_application
    from dj_static import Cling
    application = Cling(get_wsgi_application())
    

    它创建一个SITE_ROOT 变量,然后将其添加到路径中。我在我的 celery.py 文件中做了同样的事情,它可以工作。

    【讨论】:

      猜你喜欢
      • 2017-08-19
      • 2021-12-12
      • 1970-01-01
      • 2015-06-23
      • 2021-09-08
      • 2018-05-02
      • 1970-01-01
      • 2020-12-26
      • 2021-08-18
      相关资源
      最近更新 更多