在Heroku上正确引用Django设置的Django?

2024-04-24 20:43:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用django-rq在django服务器上执行一些异步处理。在遵循docs之后,我将环境设置为:

DJANGO_SETTINGS_MODULE=config.settings rqworker high default low  

但在部署之后,我从Heroku得到以下错误:

^{pr2}$

你知道如何正确引用Django设置变量吗?目前,没有这个配置,一切都在本地工作。在

文件结构:

 ncla/ 
    api/
      views.py <-- using django_rq
 config/
    setttings.py
 ProcFile
 run-worker.py
 requirements.txt

程序文件:

web: gunicorn --pythonpath="$PWD/ncla" config.wsgi:application

worker: python -u run-worker.py

跑-工人.py公司名称:

import os
import urlparse
from redis import Redis
from rq import Worker, Queue, Connection

listen = ['high', 'default', 'low']

redis_url = os.getenv('REDISTOGO_URL')
if not redis_url:
    raise RuntimeError('Set up Redis To Go first.')

urlparse.uses_netloc.append('redis')
url = urlparse.urlparse(redis_url)
conn = Redis(host=url.hostname, port=url.port, db=0, password=url.password)

with Connection(conn):
    worker = Worker(map(Queue, listen))
    worker.work()

Heroku日志:

2014-05-21T16:52:56.909996+00:00 heroku[router]: at=info method=GET path=/admin/ host=ncla-dev.herokuapp.com request_id=3bc938ba-8550-4d41-9d4a-40d46e9a1aa6 fwd="74.113.160.196" dyno=web.1 connect=2ms service=16ms status=500 bytes=238
2014-05-21T16:52:58.541487+00:00 heroku[router]: at=info method=GET path=/admin/ host=ncla-dev.herokuapp.com request_id=30a9d659-3826-402c-ac30-db2a67d21374 fwd="74.113.160.196" dyno=web.1 connect=5ms service=16ms status=500 bytes=238
2014-05-21T16:52:58.537244+00:00 app[web.1]: 2014-05-21 16:52:58 [7] [ERROR] Error handling request
2014-05-21T16:52:58.537253+00:00 app[web.1]:     respiter = self.wsgi(environ, resp.start_response)
2014-05-21T16:52:58.537256+00:00 app[web.1]:     self.load_middleware()
2014-05-21T16:52:58.537264+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 49, in _setup
2014-05-21T16:52:58.537249+00:00 app[web.1]: Traceback (most recent call last):
2014-05-21T16:52:58.537252+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 131, in handle_request
2014-05-21T16:52:58.537255+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 187, in __call__
2014-05-21T16:52:58.537258+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py", line 46, in load_middleware
2014-05-21T16:52:58.537260+00:00 app[web.1]:     for middleware_path in settings.MIDDLEWARE_CLASSES:
2014-05-21T16:52:58.537261+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
2014-05-21T16:52:58.537263+00:00 app[web.1]:     self._setup(name)
2014-05-21T16:52:58.537266+00:00 app[web.1]:     self._wrapped = Settings(settings_module)
2014-05-21T16:52:58.537267+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 132, in __init__
2014-05-21T16:52:58.537269+00:00 app[web.1]:     % (self.SETTINGS_MODULE, e)
2014-05-21T16:52:58.537271+00:00 app[web.1]: ImportError: Could not import settings 'config.settings rqworker high default low' (Is it on sys.path? Is there an import error in the settings file?): No module named settings rqworker high default low

Tags: djangoinpyimportredisappurlheroku
1条回答
网友
1楼 · 发布于 2024-04-24 20:43:54

我没有使用上面列出的方法,而是使用以下步骤成功地部署到heroku:

django-rq添加到requirements.txt文件中:

pip freeze > requirements.txt

将您的Procfile更新为:

^{pr2}$

提交并重新部署。然后添加新的工作人员:

heroku scale worker=1

对于后人来说,我也在他们的github上更新了django rq自述文件,并提供了这些信息。在

相关问题 更多 >