jukrapopk commited on
Commit
40c772f
·
1 Parent(s): 9f27d12

features: add kill + monitor endpoints

Browse files
Files changed (1) hide show
  1. app.py +25 -0
app.py CHANGED
@@ -39,6 +39,8 @@ def frame_processor(session_id: str):
39
  next_frame_time = time.time()
40
 
41
  while True:
 
 
42
  # control framerate
43
  if time.time() >= next_frame_time:
44
  ret, frame = cap.read()
@@ -115,5 +117,28 @@ def video_feed():
115
 
116
  return Response(stream_with_context(stream_mjpeg(session_id)), mimetype="multipart/x-mixed-replace; boundary=frame")
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  if __name__ == "__main__":
119
  app.run(host="0.0.0.0", port=PORT, threaded=True)
 
39
  next_frame_time = time.time()
40
 
41
  while True:
42
+ if session_id not in sessions:
43
+ break
44
  # control framerate
45
  if time.time() >= next_frame_time:
46
  ret, frame = cap.read()
 
117
 
118
  return Response(stream_with_context(stream_mjpeg(session_id)), mimetype="multipart/x-mixed-replace; boundary=frame")
119
 
120
+ @app.get("/sessions")
121
+ def get_sessions():
122
+ global sessions
123
+ return {
124
+ "sessions": [session.id for session in sessions.values()]
125
+ }
126
+
127
+ @app.get("/threads")
128
+ def get_threads():
129
+ threads = threading.enumerate()
130
+ return {
131
+ "threads_count": len(threads)
132
+ }
133
+
134
+ @app.get("/killall")
135
+ def kill_all():
136
+ global sessions
137
+ sessions.clear()
138
+ for thread in threading.enumerate():
139
+ if thread is not threading.current_thread():
140
+ thread.join(0.1)
141
+ return {"status": "all sessions cleared and threads joined"}
142
+
143
  if __name__ == "__main__":
144
  app.run(host="0.0.0.0", port=PORT, threaded=True)