我有一个 flask 应用程序如下:
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 确实提供了一个停止的包装器,当您使用它时,默认行为应该停止服务器。 https://github.com/miguelgrinberg/Flask-SocketIO/blob/master/flask_socketio/init.py#L510
因此,如果您将代码修改为类似这样的内容 -
def shutdown_server():
socketio2.stop()
然后它应该完成这项工作
关于python - 未能结束 Flask 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40807784/