【发布时间】:2021-11-03 08:06:13
【问题描述】:
我有一个 Flask 应用程序,在提交表单时,它需要启动一个新线程。我正在使用 Gunicorn 在免费的 Heroku dyno 上运行该应用程序,每次我调用 threading.Thread().start() 或 gevent.threading.Thread().start() 时,请求都会挂起并最终超时。我在下面直接放了 Heroku 日志的 sn-p,但是,基本上,一旦工人死亡,另一个似乎拿起并启动线程没问题,但这最终意味着表单提交需要很长时间并且通常需要刷新。
(Heroku 日志): 2021-09-06T07:37:56.372718+00:00 heroku[路由器]: at=info method=POST path="/form## Heading ##" host=.herokuapp.com request_id=2fcceff4-70d5-4629-bd06 -0df682c9a0a9 fwd="152.3.43.49" dyno=web.1 连接=0ms 服务=29609ms 状态=200 字节=4485 协议=http 2021-09-06T07:37:56.372291+00:00 app[web.1]: [2021-09-06 07:37:56 +0000] [4] [CRITICAL] 工作人员超时(pid:32)
到目前为止我已经尝试过:
- 同时使用
gevent和threading线程。 - 使用
gevent和eventlet运行我的Procfile,将--threads 和--worker-connections 选项设置为100,正如this 帖子所建议的那样。 - 使用 uWSGI 运行
简化的复制是这样的:
Procfile: web: gunicorn -k gevent --worker-connections 100 app:app
app.py:
app = Flask(__name__)
@app.route("/form", methods=["POST"])
def form():
t = threading.Thread(target=func, args=(request.form["code"],))
t.setDaemon(true)
t.start()
return render_template("index.html")
def func(code):
while True:
# do stuff (loop will not terminate)
【问题讨论】:
标签: python multithreading flask heroku gunicorn