Python Flask关闭事件处理程序 [英] Python Flask shutdown event handler

查看:186
本文介绍了Python Flask关闭事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flask作为REST端点,该端点将应用程序请求添加到队列中.然后,该队列由第二个线程使用.

server.py

def get_application():
    global app
    app.debug = True
    app.queue = client.Agent()
    app.queue.start()                                                                                                                                                                                                                
    return app

@app.route("/api/v1/test/", methods=["POST"])
def test():
     if request.method == "POST":
        try:
           #add the request parameters to queue
           app.queue.add_to_queue(req)
        except Exception:
            return "All the parameters must be provided" , 400
     return "", 200

     return "Resource not found",404

client.py

class Agent(threading.Thread):

      def __init__(self):
          threading.Thread.__init__(self)
          self.active = True
          self.queue = Queue.Queue(0)


      def run(self):
           while self.active:
              req = self.queue.get()
              #do something


      def add_to_queue(self,request):
           self.queue.put(request)

flask中是否有关闭事件处理程序,以便每当烧瓶应用程序关闭时(例如重新启动apache服务时),我就可以干净地关闭使用者线程?

解决方案

如果您要查找的是没有app.stop(),但是使用模块atexit,您可以执行类似的操作:

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

考虑一下:

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()

如果使用Ctrl-C强制关闭服务器,则不会调用-c2>.

为此,还有另一个模块-signal.

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

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

def get_application():
    global app
    app.debug = True
    app.queue = client.Agent()
    app.queue.start()                                                                                                                                                                                                                
    return app

@app.route("/api/v1/test/", methods=["POST"])
def test():
     if request.method == "POST":
        try:
           #add the request parameters to queue
           app.queue.add_to_queue(req)
        except Exception:
            return "All the parameters must be provided" , 400
     return "", 200

     return "Resource not found",404

client.py

class Agent(threading.Thread):

      def __init__(self):
          threading.Thread.__init__(self)
          self.active = True
          self.queue = Queue.Queue(0)


      def run(self):
           while self.active:
              req = self.queue.get()
              #do something


      def add_to_queue(self,request):
           self.queue.put(request)

Is there a shutdown event handler in flask so that I can cleanly shutdown the consumer thread whenever the flask app is shutdown (like when the apache service is restarted)?

解决方案

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

这篇关于Python Flask关闭事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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