Heroku:使用RQ的Python中的后台任务 [英] Heroku: Background Tasks in Python with RQ

查看:276
本文介绍了Heroku:使用RQ的Python中的后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在按照 Heroku文档
但是,我一开始就陷入困境。



我已成功安装RQ:

  pip install rq 

我创建了 worker.py 文件,其中包含与文档中完全相同的代码。



但是当我尝试运行时:

  python worker .py 

我收到以下错误:

 追溯(最近的最后一次呼叫):
文件/Users/MyUser/my_app/lib/python3.5/site-packages/redis/connection.py,行439,连接
sock = self._connect()
文件/Users/MyUser/my_app/lib/python3.5/site-packages/redis/connection.py,第494行,在_connect
raise err
文件/Users/MyUser/my_app/lib/python3.5/site-packages/redis/connection.py,第482行,在_connect
sock.connect(socket_address )
ConnectionRefusedError:[Errno 61]连接拒绝

在处理上述异常时,发生另一个异常:

追溯(最近的最后一次呼叫):
文件/Users/MyUser/my_app/lib/python3.5/site-packages/redis/client.py,第572行,在execute_command
connection.send_command(* args)
文件 /用户/ MYUSER / my_app应用/ LIB / python3。 5 / site-packages / redis / connection.py,第563行,在send_command
self.send_packed_command(self.pack_command(* args))
文件/ Users / MyUser / my_app / lib / python3 .5 / site-packages / redis / connection.py,第538行,在send_packed_command
self.connect()
文件/Users/MyUser/my_app/lib/python3.5/site-packages /redis/connection.py,第442行,连接
raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError:错误61连接到本地主机:6379。拒绝连接。

在处理上述异常时,发生另一个异常:

追溯(最近的最后一次调用):
文件/ Users / MyUser / my_app / lib / python3.5 / site-packages / redis / connection.py,第439行,连接
sock = self._connect()
文件/Users/MyUser/my_app/lib/python3.5/ site-packages / redis / connection.py,第494行,在_connect
raise err
文件/Users/MyUser/my_app/lib/python3.5/site-packages/redis/connection.py ,line 482,in _connect
sock.connect(socket_address)
ConnectionRefusedError:[Errno 61]连接拒绝

在处理上述异常时发生了另一个异常:

追溯(最近的最后一次调用):
文件worker.py,第15行,< module>
worker.work()
文件/Users/MyUser/my_app/lib/python3.5/site-packages/rq/worker.py,第433行,工作
self。 register_birth()
文件/Users/MyUser/my_app/lib/python3.5/site-packages/rq/worker.py,第250行,register_birth
如果self.connection.exists(self .key)和\ $​​ b $ b文件/Users/MyUser/my_app/lib/python3.5/site-packages/redis/client.py,第855行,存在
return self.execute_command( 'EXISTS',name)
文件/Users/MyUser/my_app/lib/python3.5/site-packages/redis/client.py,第578行,在execute_command
connection.send_command(* args)
文件/Users/MyUser/my_app/lib/python3.5/site-packages/redis/connection.py,第563行,在send_command
self.send_packed_command(self.pack_command(* args))
文件/Users/MyUser/my_app/lib/python3.5/site-packages/redis/connection.py,第538行,在send_packed_command
self.connect()
文件/Users/MyUser/my_app/lib/python3.5/site-packages/redis/conne ction.py,第442行,连接
raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError:错误61连接到本地主机:6379。拒绝连接。

经过一番研究,我意识到 REDISTOGO_URL 没有配置我现在安装了 redistogo ,当运行 heroku config 时,我可以找到它。不幸的是,这似乎不是我做错了的唯一的事情,因为我仍然遇到同样的错误。



我也尝试改变 localhost by 127.0.0.1 ,建议这里,但没有帮助。



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



我错过了哪些步骤?

解决方案

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



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


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?

解决方案

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.

这篇关于Heroku:使用RQ的Python中的后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆