Tôi đang sử dụng Flask làm điểm cuối REST để thêm yêu cầu ứng dụng vào hàng đợi. Hàng đợi sau đó được tiêu thụ bởi một luồng thứ hai.Trình xử lý sự kiện tắt Flask bằng Python
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)
Có một xử lý sự kiện tắt máy trong bình để tôi có thể sạch shutdown thread tiêu dùng bất cứ khi nào ứng dụng bình là tắt máy (giống như khi dịch vụ apache là khởi động lại)?
Tôi đang sử dụng tính năng này và hoạt động tốt. Cảm ơn. Nhân tiện, atexit xử lý Ctrl C đúng cách – arturvt