小编典典

Heroku:带有RQ的Python中的后台任务

redis

我正在尝试使用Heroku为我的Django应用设置后台任务。

我正在按照Heroku文档中说明的步骤进行操作。但是,我一开始就已经陷入困境。

我已经成功安装了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可以找到它。不幸的是,这似乎并不是我做错的唯一事情,因为我仍然遇到相同的错误。

我也尝试过localhost按此处127.0.0.1建议的那样更改by,但没有帮助。

在我发现的所有其他相关问题中,本地服务器上不会发生此问题。

我缺少什么步骤?


阅读 437

收藏
2020-06-20

共1个答案

小编典典

您将Heroku上的生产应用程序与本地开发环境相混淆。RedisToGo是在Heroku上配置的,但是当您在开发计算机上运行RedisToGo时,环境变量不存在,因此它会尝试查找在本地运行的Redis实例。

您可以在开发机器上安装Redis,也可以在本地配置REDISTOGO_URL环境变量。

2020-06-20