Python Flask 应用程序卡住没有响应

标签 python linux flask

我有一个在 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/

相关文章:

linux - 如何通过脚本创建 crontab

python - flask.request.form 中的动态表单字段

image - 使用 Angular http.get 调用异步加载图像

python - 基于另一个数组中的信息对 NumPy 数组进行操作

python - 在 OSX 中的另一个应用程序上触发放置事件

c - POSIX 信号量父信号量值不受影响

python - 使用 Flask 和 Swagger 公开 API 文档

python - graphlab:如何将 gzip 压缩文件加载到 SFrame 中

python - 从 python 中的 numpy genfromtxt 获取列名

使用 Apache 在树莓派 (Debian) 上运行 PHP