python – 在Heroku上正确引用django-rq的django设置?

我正在尝试使用
django-rq在我的django服务器上进行一些异步处理.在遵循
docs之后,我将环境设置为:

DJANGO_SETTINGS_MODULE=config.settings rqworker high default low  

但是在部署之后我从Heroku那里得到了这个错误:

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

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

文件结构:

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

Procfile:

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

worker: python -u run-worker.py

run-worker.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

最佳答案 我使用这些步骤成功部署到heroku,而不是上面列出的方法:

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

pip freeze > requirements.txt

将您的Procfile更新为:

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

worker: python your_app_name/manage.py rqworker high default low

提交并重新部署.然后添加新员工:

heroku scale worker=1

对于后代,我已经使用这些信息在他们的github上更新了django-rq自述文件.

点赞