简体   繁体   English

Flask / Heroku / Redis / RQ内部服务器错误

[英]Flask / Heroku / Redis / RQ Internal Server Error

I have a recommendation site. 我有一个推荐网站。 Everything was working dandy, until at points when the site was under a decent amount of traffic, the recommendations would take longer than 30 seconds (Heroku's limit) and time-out, throwing a 500 error. 一切工作都非常繁琐,直到站点流量不错时,建议将花费超过30秒(Heroku的限制)和超时,并抛出500错误。 I realize this is a very long time for a http request. 我意识到这对于http请求来说是很长的时间。

So, I read up online and implemented RQ with Redis. 因此,我在线阅读并使用Redis实现了RQ。 I got that to work, but after some testing, it will still throw the Internal Server Error, even though the requests are going through a queue. 我可以使用它,但是经过一些测试,即使请求正在排队,它仍然会引发内部服务器错误。

I'm really just lacking knowledge here and I have no idea what to do. 我真的只是这里的知识,我不知道该怎么办。 I think I'm missing the whole idea of rq and redis I guess? 我想我想念rq和redis的整个想法了吗? Here's some of my code if it helps, but I'm hoping for more of just guidance of where to go from here to fix this error. 这是我的一些代码(如果有帮助的话),但是我希望从此处修复错误的更多指导。

worker.py 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()

part of my views.py 我的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)

Any guidance is appreciated 任何指导表示赞赏

You can always try dividing the work between a web dyno to simply acknowledge the request, then have worker dynos doing the heavy lifting. 您总是可以尝试在网络dyno之间划分工作,以简单地确认请求,然后让工人dyno进行繁重的工作。

Kafka or something similar could be used to accomplish this. Kafka或类似的东西可以用来完成这个任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM