非常感谢有经验的人将redis配置为heroku上的芹菜经纪人django项目的后端的经验。我的任务调度在本地主机上运行良好,但是我发现将其部署在heroku上确实令人沮丧:
我遵循redistogo文档(https://devcenter.heroku.com/articles/redistogo#install-redis-in-python)来配置settings.py,或者,还尝试实现解决方案here的变体。都不为我工作。这是我在settings.py中拥有的东西:
redis_url = os.environ.get('REDISTOGO_URL', 'http://localhost:6959')
CACHES = {
'default': {
'BACKEND': 'redis_cache.RedisCache',,
'LOCATION': '%s:%s' % (redis_url.hostname, redis_url.port),
'OPTIONS': {
'DB': 0, # or 1?
'PASSWORD': redis_url.password,
#'PARSER_CLASS': 'redis.connection.HiredisParser'
},
},
}
CELERY_RESULT_BACKEND = redis_url
BROKER_URL = 'redis://localhost:6959/0'
[当我尝试运行该应用时,这是我的heroku日志:
2013-07-11T12:16:10.998516+00:00 app[web.1]: apps = settings.INSTALLED_APPS
2013-07-11T12:16:10.998516+00:00 app[web.1]: mod = importlib.import_module(self.SETTINGS_MODULE)
2013-07-11T12:16:10.998263+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
2013-07-11T12:16:10.998263+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 263, in fetch_command
2013-07-11T12:16:10.998516+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
2013-07-11T12:16:10.998516+00:00 app[web.1]: self._setup(name)
2013-07-11T12:16:10.998516+00:00 app[web.1]: self._wrapped = Settings(settings_module)
2013-07-11T12:16:10.998516+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 132, in __init__
2013-07-11T12:16:10.998516+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
2013-07-11T12:16:10.998516+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
2013-07-11T12:16:10.998712+00:00 app[web.1]: 'LOCATION': '%s:%s' % (redis_url.hostname, redis_url.port),
2013-07-11T12:16:10.998712+00:00 app[web.1]: AttributeError: 'str' object has no attribute 'hostname'
2013-07-11T12:16:12.201202+00:00 heroku[web.1]: Process exited with status 1
2013-07-11T12:16:12.250743+00:00 heroku[web.1]: State changed from starting to crashed
我如何将redis_url当作URI而不是str对待?
我的procfile:
web: python manage.py run_gunicorn -b 0.0.0.0:$PORT -w 3 --log-level info
scheduler: python manage.py celeryd -B -E
worker: python manage.py celeryd -E -B --loglevel=INFO
在要求中,我有django-redis-cache == 0.10.0,redis == 2.7.6,django-celery == 3.0.17,celery celery == 3.0.20和kombu == 2.5.12
看起来像os.environ.get
返回一个字符串(或str
?对Python不熟悉),并且您期望它更像URI对象或其他东西。普通的python字符串会响应hostname
之类的方法吗?
The documentation也有此步骤:
redis = redis.from_url(redis_url)
[将字符串解析为redis对象的according to these docs。
我对自己的解决方案并不十分满意,但是我对redistogo主机名,端口和密码进行了硬编码,现在一切运行顺利。
使用python urlparse库。它将URL解析为组件。
redis_url = os.environ.get('REDISTOGO_URL', 'http://localhost:6959')
redis_url = urlparse.urlparse(redis_url)