【问题标题】:How to resolve rq multiple tasks h12 heroku timeout?如何解决 rq 多任务 h12 heroku 超时?
【发布时间】:2019-07-30 00:56:22
【问题描述】:

我正在尝试使用 redis 运行 heroku flask python 应用程序。但是,我面临以下错误:

at=error code=H12 desc="Request timeout" method=POST path="/uploaderlocal" host=a2n.herokuapp.com dyno=web.1 connect=1ms service=31890ms status=503 bytes=0 protocol=https

heroku 网页因上述错误而崩溃(由于超时),但后台进程将继续运行。在redis后台队列中的任务完成之前,是否有任何可能的方法来停止heroku网页?以下是我的代码供您参考。请注意,我正在尝试在返回 results.html 模板之前完成多个 redis 排队任务。

from rq import Queue

from worker import conn

from rq.job import Job
app = Flask(__name__)
q = Queue(connection=conn)

job1 = q.enqueue_call(func=method1(), args=(), timeout='1h')

job2 = q.enqueue_call(func=method2(), args=(), timeout='1h')

job3 = q.enqueue_call(func=method3(), args=(), timeout='1h')

job4 = q.enqueue_call(func=method4(), args=(), timeout='1h')

the below code attempts (but fails) to stall the return of the #html template before the processes are finished

while(len(q)>0):

time.sleep(1)

return render_template('result.html')

提前感谢您的帮助。

【问题讨论】:

  • 我只是想澄清我的问题。我在问是否有办法检查redis队列任务何时成功完成,以便在所有排队任务完成后我可以适当地继续。

标签: python heroku flask redis


【解决方案1】:

我想你必须在 time.sleep(1) 命令前面有一些空格?

变量 q 的值在 while 循环内的任何时候都不会改变。因此它将永久休眠,或直接退出。

你需要在while循环中重新计算q,可能如下:

    while True:
        time.sleep(1)
        q = Queue(connection=conn)
        if len(q) == 0:      
            break

【讨论】:

  • 根据python-rq.org/docs,您正在正确检查 q 的长度,但是您在程序中检查了错误的位置。您在向 q 添加任何内容之前检查长度,因此代码中 q 的值将始终为零。
猜你喜欢
  • 2013-02-07
  • 2021-06-07
  • 2012-07-08
  • 1970-01-01
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 2012-06-14
  • 1970-01-01
相关资源
最近更新 更多