Python Flask应用程序无法响应

Python Flask应用程序无法响应,python,linux,flask,Python,Linux,Flask,我在linux服务器上运行了一个Flask应用程序,并注意到它在向其发送POST请求时偶尔会卡住,然后转到get并再次尝试POST(然后卡住)。如果我再次得到它,它就会“不被卡住”(然后最后一篇被卡住的帖子就完成了) 烧瓶应用程序的第一部分是: @app.route('/myroute', methods=['GET','POST']) def myfunction(): if request.method == 'POST': ... else:

我在linux服务器上运行了一个Flask应用程序,并注意到它在向其发送POST请求时偶尔会卡住,然后转到get并再次尝试POST(然后卡住)。如果我再次得到它,它就会“不被卡住”(然后最后一篇被卡住的帖子就完成了)

烧瓶应用程序的第一部分是:

@app.route('/myroute', methods=['GET','POST'])
def myfunction():
    if request.method == 'POST':
        ...
    else:
        ...
从以下内容开始:
FLASK\u APP=myflask.py FLASK\u 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…
启动应用程序时未运行“\uuuuuu name\uuuuuuu==”\uuuuuu main\uuuuuuu”则
中的代码

因此
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.

随Flask提供的WSGI服务器仅推荐用于开发目的,不适合于重载请求。因为您提到了Linux服务器,所以使用它是一个很好的实践。Gunicorn速度快,服务器资源少。使用
pip install gunicorn
安装后,当应用程序继续收到更高的请求时,您可以轻松分配工作线程。下面的示例分配了4个工作进程(
-w4

gunicorn-w4-b127.0.0.1:4000 myproject:app


您可以在官方网站上找到类似独立WSGI容器的更多信息。

谢谢。您知道如何使用gunicorn设置并始终运行Flask应用程序(即使在服务器重新启动后)?您可以查看其中的一些,但我在这里只使用了很少的标志。一般来说,Gunicorn服务器是持久的,可以使用配置文件调整设置。我的建议是安排一个cron作业或shell脚本,在Linux服务器重新启动或引导后启动Gunicorn服务器(Flask应用程序)。