Python Flask shutdown event handler

前端 未结 1 1432
心在旅途
心在旅途 2020-12-07 00:25

I\'m using Flask as a REST endpoint which adds an application request to a queue. The queue is then consumed by a second thread.

server.py



        
1条回答
  •  一生所求
    2020-12-07 00:55

    There is no app.stop() if that is what you are looking for, however using module atexit you can do something similar:

    https://docs.python.org/2/library/atexit.html

    Consider this:

    import atexit
    #defining function to run on shutdown
    def close_running_threads():
        for thread in the_threads:
            thread.join()
        print "Threads complete, ready to finish"
    #Register the function to be called on exit
    atexit.register(close_running_threads)
    #start your process
    app.run()
    

    Also of note-atexit will not be called if you force your server down using Ctrl-C.

    For that there is another module- signal.

    https://docs.python.org/2/library/signal.html

    0 讨论(0)
提交回复
热议问题