非常感谢任何有经验将redis配置为heroku上芹菜代理的django项目后端的人的意见。我的任务调度从localhost运行良好,但我发现将它部署在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
答案 0 :(得分:1)
使用python urlparse库。它将URL解析为组件。
redis_url = os.environ.get('REDISTOGO_URL', 'http://localhost:6959')
redis_url = urlparse.urlparse(redis_url)
答案 1 :(得分:0)
看起来os.environ.get
正在返回一个字符串(或str
?不熟悉python),并且你希望它更像是一个URI对象。普通的python字符串是否会响应hostname
等方法?
The documentation也有这一步:
redis = redis.from_url(redis_url)
其中according to these docs将字符串解析为redis对象。
答案 2 :(得分:0)
我对我的解决方案并不满意,但我对redistogo主机名,端口和密码进行了硬编码,现在一切运行顺利。