Heroku:使用 RQ 的 Python 后台任务
Posted
技术标签:
【中文标题】Heroku:使用 RQ 的 Python 后台任务【英文标题】:Heroku: Background Tasks in Python with RQ 【发布时间】:2017-09-29 16:36:03 【问题描述】:我正在尝试使用 Heroku 为我的 Django 应用设置后台任务。
我正在按照Heroku documentation 中说明的步骤进行操作。 但是,我一开始就被卡住了。
我已成功安装 RQ:
pip install rq
我创建了worker.py
文件,其中包含与文档中完全相同的代码。
但是,当我尝试运行时:
python worker.py
我收到以下错误:
Traceback (most recent call last):
File "/Users/MyUser/my_app/lib/python3.5/site-packages/redis/connection.py", line 439, in connect
sock = self._connect()
File "/Users/MyUser/my_app/lib/python3.5/site-packages/redis/connection.py", line 494, in _connect
raise err
File "/Users/MyUser/my_app/lib/python3.5/site-packages/redis/connection.py", line 482, in _connect
sock.connect(socket_address)
ConnectionRefusedError: [Errno 61] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/MyUser/my_app/lib/python3.5/site-packages/redis/client.py", line 572, in execute_command
connection.send_command(*args)
File "/Users/MyUser/my_app/lib/python3.5/site-packages/redis/connection.py", line 563, in send_command
self.send_packed_command(self.pack_command(*args))
File "/Users/MyUser/my_app/lib/python3.5/site-packages/redis/connection.py", line 538, in send_packed_command
self.connect()
File "/Users/MyUser/my_app/lib/python3.5/site-packages/redis/connection.py", line 442, in connect
raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 61 connecting to localhost:6379. Connection refused.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/MyUser/my_app/lib/python3.5/site-packages/redis/connection.py", line 439, in connect
sock = self._connect()
File "/Users/MyUser/my_app/lib/python3.5/site-packages/redis/connection.py", line 494, in _connect
raise err
File "/Users/MyUser/my_app/lib/python3.5/site-packages/redis/connection.py", line 482, in _connect
sock.connect(socket_address)
ConnectionRefusedError: [Errno 61] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "worker.py", line 15, in <module>
worker.work()
File "/Users/MyUser/my_app/lib/python3.5/site-packages/rq/worker.py", line 433, in work
self.register_birth()
File "/Users/MyUser/my_app/lib/python3.5/site-packages/rq/worker.py", line 250, in register_birth
if self.connection.exists(self.key) and \
File "/Users/MyUser/my_app/lib/python3.5/site-packages/redis/client.py", line 855, in exists
return self.execute_command('EXISTS', name)
File "/Users/MyUser/my_app/lib/python3.5/site-packages/redis/client.py", line 578, in execute_command
connection.send_command(*args)
File "/Users/MyUser/my_app/lib/python3.5/site-packages/redis/connection.py", line 563, in send_command
self.send_packed_command(self.pack_command(*args))
File "/Users/MyUser/my_app/lib/python3.5/site-packages/redis/connection.py", line 538, in send_packed_command
self.connect()
File "/Users/MyUser/my_app/lib/python3.5/site-packages/redis/connection.py", line 442, in connect
raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 61 connecting to localhost:6379. Connection refused.
经过一番研究,我意识到REDISTOGO_URL
没有配置。我现在已经安装了redistogo
,在运行heroku config
时我可以找到它。不幸的是,这似乎不是我做错的唯一事情,因为我仍然遇到同样的错误。
我也尝试按照here 的建议将localhost
更改为127.0.0.1
,但没有帮助。
在我发现的所有其他相关问题中,本地服务器上都没有出现问题。
我缺少哪些步骤?
【问题讨论】:
【参考方案1】:您将 Heroku 上的生产应用程序与本地开发环境混淆了。 RedisToGo 是在 Heroku 上配置的,但是当您在开发机器上运行它时,环境变量不存在,因此它会尝试查找本地运行的 Redis 实例。
您可以在开发机器上安装 Redis,也可以在本地配置 REDISTOGO_URL 环境变量。
【讨论】:
我以为这是我在本地开发环境中运行pip install rq
时完成的...你能告诉我应该如何安装它或如何在本地配置 REDISTOGO_URL 环境变量吗?
好的,我想出了一个办法:我使用了运行命令heroku config
时得到的REDISTOGO_URL
而不是redis://localhost:6379
,它似乎正在工作。谢谢!以上是关于Heroku:使用 RQ 的 Python 后台任务的主要内容,如果未能解决你的问题,请参考以下文章
Celery / Heroku - 使用 Heroku run python 在后台运行时,延迟()啥也不做
在Django中测试django-rq(python-rq)的最佳实践