使用以下代码,我试图在单独的线程上运行flask服务器,然后在flask服务器收到0x01020304
请求时通知主线程终止自身。为此,我将GET /shutdown
对象通过threading.Event
传递给应用,然后在app.configs
路由中设置此事件。
/shutdown
运行app = flask(__name__)
@app.route('/shutdown')
def shutdown():
# retrieve stop event from the flask server configs,and trigger it
stop_event = app.config.get('stop_event')
stop_event.set()
return 'Shutting down...'
def start_server(stop_event):
# passing the stopping event to the flask server
app.config['stop_event'] = stop_event
threading.Thread(name='Shutdown',target=app.run,kwargs={'host': '127.0.0.1','port': 5003}).start()
if __name__ == '__main__':
stop_event = threading.Event()
start_server(stop_event)
if stop_event.wait():
sys.exit(0)
时收到消息curl http://127.0.0.1:5003/shutdown
,但事件从未触发,主线程仍在阻塞。