问题:如何在不使用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.


回答 0

如果只是在桌面上运行服务器,则可以公开终结点以终止服务器(有关更多信息,请参见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...'

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

from multiprocessing import Process

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

让我知道是否有帮助。

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

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

Let me know if this helps.


回答 1

我使用线程做了一点不同

from werkzeug.serving import make_server

class ServerThread(threading.Thread):

    def __init__(self, app):
        threading.Thread.__init__(self)
        self.srv = make_server('127.0.0.1', 5000, app)
        self.ctx = app.app_context()
        self.ctx.push()

    def run(self):
        log.info('starting server')
        self.srv.serve_forever()

    def shutdown(self):
        self.srv.shutdown()

def start_server():
    global server
    app = flask.Flask('myapp')
    ...
    server = ServerThread(app)
    server.start()
    log.info('server started')

def stop_server():
    global server
    server.shutdown()

我用它来进行静态API的端到端测试,在这里我可以使用python请求库发送请求。

I did it slightly different using threads

from werkzeug.serving import make_server

class ServerThread(threading.Thread):

    def __init__(self, app):
        threading.Thread.__init__(self)
        self.srv = make_server('127.0.0.1', 5000, app)
        self.ctx = app.app_context()
        self.ctx.push()

    def run(self):
        log.info('starting server')
        self.srv.serve_forever()

    def shutdown(self):
        self.srv.shutdown()

def start_server():
    global server
    app = flask.Flask('myapp')
    ...
    server = ServerThread(app)
    server.start()
    log.info('server started')

def stop_server():
    global server
    server.shutdown()

I use it to do end to end tests for restful api, where I can send requests using the python requests library.


回答 2

这是一个有点旧的线程,但是如果有人从后台运行的脚本开始尝试,学习或测试基本的flask应用,则停止它的最快方法是终止正在运行您的应用的端口上运行的进程上。注意:我知道作者正在寻找一种不杀死或停止该应用程序的方式。但这可能会对正在学习的人有所帮助。

sudo netstat -tulnp | grep :5001

你会得到这样的东西。

tcp 0 0 0.0.0.0:5001 0.0.0.0:* LISTEN 28834 / python

要停止应用,请终止进程

sudo kill 28834

This is a bit old thread, but if someone experimenting, learning, or testing basic flask app, started from a script that runs in the background, the quickest way to stop it is to kill the process running on the port you are running your app on. Note: I am aware the author is looking for a way not to kill or stop the app. But this may help someone who is learning.

sudo netstat -tulnp | grep :5001

You’ll get something like this.

tcp 0 0 0.0.0.0:5001 0.0.0.0:* LISTEN 28834/python

To stop the app, kill the process

sudo kill 28834

回答 3

我的方法可以通过bash终端/控制台进行

1)运行并获取进程号

$ ps aux | grep yourAppKeywords

2a)终止进程

$ kill processNum

2b)如果以上方法无效,则终止该进程

$ kill -9 processNum

My method can be proceeded via bash terminal/console

1) run and get the process number

$ ps aux | grep yourAppKeywords

2a) kill the process

$ kill processNum

2b) kill the process if above not working

$ kill -9 processNum

回答 4

正如其他人指出的那样,您只能werkzeug.server.shutdown在请求处理程序中使用。我发现在另一时间关闭服务器的唯一方法是向自己发送请求。例如,/kill此片段中的处理程序将杀死开发服务器,除非在下一秒内收到另一个请求:

import requests
from threading import Timer
from flask import request
import time

LAST_REQUEST_MS = 0
@app.before_request
def update_last_request_ms():
    global LAST_REQUEST_MS
    LAST_REQUEST_MS = time.time() * 1000


@app.route('/seriouslykill', methods=['POST'])
def seriouslykill():
    func = request.environ.get('werkzeug.server.shutdown')
    if func is None:
        raise RuntimeError('Not running with the Werkzeug Server')
    func()
    return "Shutting down..."


@app.route('/kill', methods=['POST'])
def kill():
    last_ms = LAST_REQUEST_MS
    def shutdown():
        if LAST_REQUEST_MS <= last_ms:  # subsequent requests abort shutdown
            requests.post('http://localhost:5000/seriouslykill')
        else:
            pass

    Timer(1.0, shutdown).start()  # wait 1 second
    return "Shutting down..."

As others have pointed out, you can only use werkzeug.server.shutdown from a request handler. The only way I’ve found to shut down the server at another time is to send a request to yourself. For example, the /kill handler in this snippet will kill the dev server unless another request comes in during the next second:

import requests
from threading import Timer
from flask import request
import time

LAST_REQUEST_MS = 0
@app.before_request
def update_last_request_ms():
    global LAST_REQUEST_MS
    LAST_REQUEST_MS = time.time() * 1000


@app.route('/seriouslykill', methods=['POST'])
def seriouslykill():
    func = request.environ.get('werkzeug.server.shutdown')
    if func is None:
        raise RuntimeError('Not running with the Werkzeug Server')
    func()
    return "Shutting down..."


@app.route('/kill', methods=['POST'])
def kill():
    last_ms = LAST_REQUEST_MS
    def shutdown():
        if LAST_REQUEST_MS <= last_ms:  # subsequent requests abort shutdown
            requests.post('http://localhost:5000/seriouslykill')
        else:
            pass

    Timer(1.0, shutdown).start()  # wait 1 second
    return "Shutting down..."

回答 5

这是一个古老的问题,但是谷歌搜索并没有给我任何有关如何完成此操作的见识。

因为我在这里没有正确阅读代码!(Do!)它的作用是在……中RuntimeError不存在时引发一个。werkzeug.server.shutdownrequest.environ

因此,如果没有,我们可以做的request就是提高RuntimeError

def shutdown():
    raise RuntimeError("Server going down")

并在app.run()返回时捕捉到:

...
try:
    app.run(host="0.0.0.0")
except RuntimeError, msg:
    if str(msg) == "Server going down":
        pass # or whatever you want to do when the server goes down
    else:
        # appropriate handling/logging of other runtime errors
# and so on
...

无需向自己发送请求。

This is an old question, but googling didn’t give me any insight in how to accomplish this.

Because I didn’t read the code here properly! (Doh!) What it does is to raise a RuntimeError when there is no werkzeug.server.shutdown in the request.environ

So what we can do when there is no request is to raise a RuntimeError

def shutdown():
    raise RuntimeError("Server going down")

and catch that when app.run() returns:

...
try:
    app.run(host="0.0.0.0")
except RuntimeError, msg:
    if str(msg) == "Server going down":
        pass # or whatever you want to do when the server goes down
    else:
        # appropriate handling/logging of other runtime errors
# and so on
...

No need to send yourself a request.


回答 6

您不必按“ CTRL-C”,但是您可以提供一个可以为您执行此操作的端点:

from flask import Flask, jsonify, request
import json, os, signal

@app.route('/stopServer', methods=['GET'])
def stopServer():
    os.kill(os.getpid(), signal.SIGINT)
    return jsonify({ "success": True, "message": "Server is shutting down..." })

现在,您只需调用此端点即可正常关​​闭服务器:

curl localhost:5000/stopServer

You don’t have to press “CTRL-C”, but you can provide an endpoint which does it for you:

from flask import Flask, jsonify, request
import json, os, signal

@app.route('/stopServer', methods=['GET'])
def stopServer():
    os.kill(os.getpid(), signal.SIGINT)
    return jsonify({ "success": True, "message": "Server is shutting down..." })

Now you can just call this endpoint to gracefully shutdown the server:

curl localhost:5000/stopServer

回答 7

您可以使用以下方法

app.do_teardown_appcontext()

You can use method bellow

app.do_teardown_appcontext()

回答 8

如果您使用的是CLI,并且仅运行一个Flask应用程序/进程(或者,您只想终止系统上运行的任何 flask进程),则可以使用以下命令将其终止:

kill $(pgrep -f flask)

If you’re working on the CLI and only have one flask app/process running (or rather, you just want want to kill any flask process running on your system), you can kill it with:

kill $(pgrep -f flask)


回答 9

如果您不在请求响应处理之外,则仍然可以:

import os
import signal

sig = getattr(signal, "SIGKILL", signal.SIGTERM)
os.kill(os.getpid(), sig)

If you’re outside the request-response handling, you can still:

import os
import signal

sig = getattr(signal, "SIGKILL", signal.SIGTERM)
os.kill(os.getpid(), sig)

回答 10

Google Cloud VM实例+ Flask应用

我将Flask应用程序托管在Google Cloud Platform虚拟机上。我使用来启动应用程序,python main.py但是问题是ctrl + c无法停止服务器。

此命令$ sudo netstat -tulnp | grep :5000终止服务器。

默认情况下,我的Flask应用程序在端口5000上运行。

注意:我的VM实例正在Linux 9上运行。

为此。尚未针对其他平台进行测试。如果它也适用于其他版本,请随时进行更新或评论。

Google Cloud VM instance + Flask App

I hosted my Flask Application on Google Cloud Platform Virtual Machine. I started the app using python main.py But the problem was ctrl+c did not work to stop the server.

This command $ sudo netstat -tulnp | grep :5000 terminates the server.

My Flask app runs on port 5000 by default.

Note: My VM instance is running on Linux 9.

It works for this. Haven’t tested for other platforms. Feel free to update or comment if it works for other versions too.


回答 11

对于Windows,停止/杀死Flask服务器非常容易-

  1. 转到任务管理器
  2. 查找flask.exe
  3. 选择并结束流程

For Windows, it is quite easy to stop/kill flask server –

  1. Goto Task Manager
  2. Find flask.exe
  3. Select and End process

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。