celery .env变量未在settings.py中使用

时间:2017-08-26 11:07:44

标签: python django celery

我仍然坚持使用settings.py中我的.env文件中的配置变量来获取我的celery.py。

当我硬编码var SourceSystemToBeInserted = this.TxtCreateAgencySourceSystem.Text.Split(',').Select(x => x.Trim()) .Where(x => !string.IsNullOrWhiteSpace(x)) .ToArray(); var agencySourceSystemDataOwnerToBeInsertedIntoGovernance = SourceSystemToBeInserted .Select(AgencySourceSystemToBeInserted => new Governance() { AgencyCode = AgencyCode + "_" + AgencySourceSystemToBeInserted, GovernanceCode = AgencyCode + "_" + AgencySourceSystemToBeInserted + "_" + Constants.GOVERNANCE_ROLE_DATA_OWNER_VALUE, RoleCode = Constants.GOVERNANCE_ROLE_DATA_OWNER_VALUE, CreatedBy = EDHSession.Current.User.EmailAddress, CreatedDate = dateTimeNow }) .Select(agencyGovernance => new { Governance = agencyGovernance, IsAdded = userServiceProxy.CreateGovernance(agencyGovernance) }) 时,一切正常,但是,当我使用CELERY_BROKER_URL = 'redis://localhost'时,REDIS_URL不会被接管,我会收到错误。

celery.py:

CELERY_BROKER_URL= os.environ.get('REDIS_URL')

.ENV:

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ['DJANGO_SETTINGS_MODULE'] = 'xlink.settings'

app = Celery('xlink')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


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

NOT WORKING settings.py版本1:

REDIS_URL = 'redis://localhost'
REDIS_PASSWORD = 'yow'
REDIS_HOST = 'localhost'

错误版本1(试图接受默认的redis设置,因为注意到了):

CELERY_BROKER_URL= os.environ.get('REDIS_URL')
CELERY_RESULT_BACKEND= os.environ.get('REDIS_URL')

NOT WORKING settings.py第2版:

[2017-08-26 10:57:09,253: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 61] Connection refused.
Trying again in 2.00 seconds...

错误版本2(显然没有接管.env变量):

CELERY_BROKER_URL = 'redis://:{}@{}:6379/0'.format(
    os.environ.get('REDIS_PASSWORD'),
    os.environ.get('REDIS_HOST'))

WORKING settings.py:

[2017-08-26 11:11:03,419: ERROR/MainProcess] consumer: Cannot connect to redis://:**@none:6379/0: Error 8 connecting to none:6379. nodename nor servname provided, or not known..
Trying again in 2.00 seconds...

tasks.py:

CELERY_BROKER_URL= 'redis://localhost'
CELERY_RESULT_BACKEND= 'redis://localhost'

1 个答案:

答案 0 :(得分:1)

我犯的一个大错误是关于使用以下代码运行与命令行分开的芹菜工作者:

celery -A xlink worker -l info

将这行代码添加到procfile并使用heroku命令运行解决了这个问题。

配置文件:

web: gunicorn xlink.wsgi --threads 4 --log-level debug
worker: celery -A xlink worker -l info

命令让它运行:

heroku local worker

如果您想在一个终端窗口中同时运行worker和Web应用程序,只需执行heroku local并在两个终端窗口中同时启动。

感谢@Daniel Roseman的提示