如何停止瓶应用程序而不使用ctrl-c [英] How to stop flask application without using ctrl-c

查看:287
本文介绍了如何停止瓶应用程序而不使用ctrl-c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用flask-script来实现一个可以停止应用程序的命令。
我已经搜索了一段时间的解决方案。由于框架不提供app.stop()API,我很好奇如何编码。我正在使用Ubuntu 12.10和Python 2.7.3。

解决方案

如果您只是在桌面上运行服务器,公开端点来终止服务器(请阅读关闭简单服务器) (b)>

  from flask import request 
def shutdown_server():
func = request.environ.get('werkzeug。 server.shutdown')
如果func是None:
raise RuntimeError('不与Werkzeug服务器一起运行)
func()

@ app.route '$ shutdown'():
shutdown_server()
return'Server shutting down ...'


$ b

以下是另一种方法:

  from multiprocessing import Process 
$ b $ def run_server():
app.run()

server = Process(target = app.run)
server.start()
#...
server.terminate()
server.join()

让我知道这是否有帮助。


I want to implement a command which can stop flask application by using flask-script. I have searched the solution for a while. Because the framework doesn't provide "app.stop()" API, I am curious about how to code this. I am working on Ubuntu 12.10 and Python 2.7.3.

解决方案

If you are just running the server on your desktop, you can expose an endpoint to kill the server (read more at Shutdown The Simple Server):

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

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

Here is another approach that is more contained:

from multiprocessing import Process

def run_server():
    app.run()

server = Process(target=app.run)
server.start()
# ...
server.terminate()
server.join()

Let me know if this helps.

这篇关于如何停止瓶应用程序而不使用ctrl-c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆