如何在不使用 ctrl-c 的情况下停止烧瓶应用程序 [英] How to stop flask application without using ctrl-c

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

问题描述

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

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.get('/shutdown')
def shutdown():
    shutdown_server()
    return 'Server shutting down...'
    

这是另一种更包含的方法:

Here is another approach that is more contained:

from multiprocessing import Process

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

如果这有帮助,请告诉我.

Let me know if this helps.

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

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