[Python] Python Flask app not exiting with sys exit [duplicate]

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'd like to be able to terminate my flask app remotely with an http request like this:

    import flask
    import sys

    master = flask.Flask(__name__)

    @master.route('/shutdown')
    def shutdown():
    #do things
    sys.exit()

    if __name__ == '__main__':
    master.run()


    Thing is it just doesn't work. From the terminal I get nothing, as if it's not even processing the request. I know that sys.exit() just raises a SystemExit, so I think it may be that it gets caught somewhere. The fact that os._exit(0) does work also leads me to think so.

    Am I tripping on something stupid? Is it actually a bug and there is a workaround? I'd prefer not to use os._exit(0) if possible. Thanks!

    Edit: I wouldn't say this question is a duplicate since the accepted answers differ and the other one is from '13 (Flask's gone a long way in the meantime)

    Continue reading...

Compartilhe esta Página