[英]Flask / Heroku / Redis / RQ Internal Server Error
我有一个推荐网站。 一切工作都非常繁琐,直到站点流量不错时,建议将花费超过30秒(Heroku的限制)和超时,并抛出500错误。 我意识到这对于http请求来说是很长的时间。
因此,我在线阅读并使用Redis实现了RQ。 我可以使用它,但是经过一些测试,即使请求正在排队,它仍然会引发内部服务器错误。
我真的只是这里的知识,我不知道该怎么办。 我想我想念rq和redis的整个想法了吗? 这是我的一些代码(如果有帮助的话),但是我希望从此处修复错误的更多指导。
worker.py
import os
import redis
from rq import Worker, Queue, Connection
listen = ['high', 'default', 'low']
redis_url = os.getenv('REDISTOGO_URL',
'redis://redistogo:sampleurl:portNo/')
if not redis_url:
raise RuntimeError('Set up Redis to go first.')
conn = redis.from_url(redis_url)
if __name__=='__main__':
with Connection(conn):
worker = Worker(map(Queue, listen))
worker.work()
我的views.py的一部分
q = Queue(connection=conn)
@app.route('/')
def home():
form = ArtistsForm()
return render_template('home.html', form=form)
@app.route('/results', methods=['POST'])
def results():
form = ArtistsForm()
error = None
try:
if request.method == 'POST' and form.validate():
table = 'Artists'
artists = []
for value in form.data.items():
if (value[1] is not ''):
artists.append(value[1])
results = q.enqueue_call(func=getArtists, args=(table, *artists))
while results.result is None:
time.sleep(1)
results = results.result.values.tolist()
return render_template('results.html', results=results)
else:
error = "Please be sure to enter 5 artists with correct spelling" \
" and punctuation"
except pylast.WSError:
return render_template('error.html')
return render_template('home.html', form=form, error=error)
任何指导表示赞赏
您总是可以尝试在网络dyno之间划分工作,以简单地确认请求,然后让工人dyno进行繁重的工作。
Kafka或类似的东西可以用来完成这个任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.