我有一个在 Linux 服务器上运行的 Flask 应用程序,我注意到它偶尔会在向它发送 POST 请求时卡住,然后转到 GET 并再次尝试 POST(然后它会卡住)。如果我再次执行 GET,它将“解除卡住”(然后完成卡住的最后一个 POST)。
Flask 应用的第一部分是:
@app.route('/myroute', methods=['GET','POST'])
def myfunction():
if request.method == 'POST':
...
else:
...
开始于:
FLASK_APP=myflask.py FLASK_DEBUG=1 python -m flask run --port 8300 --host=0.0.0.0 --no-reload
.
还设置了并行线程:
if __name__ == '__main__':
app.run(threaded=True)
但这并不能防止卡住。
最佳答案
当您使用 python -m flask run ...
启动应用程序时,if __name__ == '__main__'
中的代码未运行。
因此 threaded=True
部分无效。
使用 --with-threads
命令行开关。
$ flask run --help
Usage: flask run [OPTIONS]
Runs a local development server for the Flask application.
This local server is recommended for development purposes only but it can
also be used for simple intranet deployments. By default it will not
support any sort of concurrency at all to simplify debugging. This can be
changed with the --with-threads option which will enable basic
multithreading.
The reloader and debugger are by default enabled if the debug flag of
Flask is enabled and disabled otherwise.
Options:
-h, --host TEXT The interface to bind to.
-p, --port INTEGER The port to bind to.
--reload / --no-reload Enable or disable the reloader. By default
the reloader is active if debug is enabled.
--debugger / --no-debugger Enable or disable the debugger. By default
the debugger is active if debug is enabled.
--eager-loading / --lazy-loader
Enable or disable eager loading. By default
eager loading is enabled if the reloader is
disabled.
--with-threads / --without-threads
Enable or disable multithreading.
--help Show this message and exit.
关于Python Flask 应用程序卡住没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53072048/