Heroku: Background Tasks in Python with RQ

2019-09-15 01:10发布

问题:

I am trying to set a background task for my Django app with Heroku.

I am following the steps explained in the Heroku documentation. However, I got stuck already at the very beginning.

I have installed RQ successfully:

pip install rq

I created the worker.py file, containing exactly the same code as in the documentation.

But then, when I try to run:

python worker.py

I get the following error:

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.

After some research, I realized that REDISTOGO_URL was not configured. I have now installed redistogo, and when running heroku config I can find it. Unfortunately, this seems not to be the only thing I was doing wrong, as I am still getting the same error.

I tried also changing localhost by 127.0.0.1, as suggested here, but it did not help.

In all other related questions I found, the problem does not happen on the local server.

What steps am I missing?

回答1:

You're confusing your production app on Heroku with your local development environment. RedisToGo is configured on Heroku, but when you run it on your development machine the environment variable is not present so it tries to find an instance of Redis running locally.

You can either install Redis on your development machine, or configure the REDISTOGO_URL environment variable locally.