未能结束 Flask 应用

Failure to end Flask application

我有一个烧瓶应用如下:

socketio2.run(app2, host="0.0.0.0", port=4998)

我创建了一个结束方法如下:

def shutdown_server():
    func = request.environ.get('werkzeug.server.shutdown')
    if func is None:
        raise RuntimeError('Not running with the Werkzeug Server')
    func()

@app2.route('/shutdown', methods=['POST'])
def shutdown():
    shutdown_server()
    print " shutdown"
    return 'Server shutting down...'

我收到以下错误

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

我的目标是结束这个 Flask 应用程序,然后 运行 一个 Python 脚本。有什么建议吗?

由于你是运行服务器作为socketio服务器,默认关闭服务器的代码将不起作用。但是,socketio 确实提供了一个包装器来停止,当您使用它时,默认行为应该停止服务器。 https://github.com/miguelgrinberg/Flask-SocketIO/blob/master/flask_socketio/init.py#L510

因此,如果您将代码修改为类似这样的内容 -

def shutdown_server():
    socketio2.stop()

那么它应该完成工作