jukrapopk commited on
Commit
e62e725
·
1 Parent(s): 11d27a3

refactor: for sessions use dictionary instead of set

Browse files
Files changed (1) hide show
  1. app.py +6 -6
app.py CHANGED
@@ -18,12 +18,12 @@ class Session:
18
  app = Flask(__name__)
19
  PORT = int(os.environ.get("PORT", 5000))
20
 
21
- sessions: set[Session] = set()
22
 
23
  def frame_processor(session_id: str):
24
  global sessions
25
 
26
- session: Session = next((s for s in sessions if s.id == session_id), None)
27
 
28
  if session is not None:
29
  cap = cv2.VideoCapture("classroom.mp4")
@@ -82,7 +82,7 @@ def frame_processor(session_id: str):
82
  def stream_mjpeg(session_id: str):
83
  global sessions
84
 
85
- session: Session = next((s for s in sessions if s.id == session_id), None)
86
 
87
  if session is not None:
88
  while True:
@@ -105,12 +105,12 @@ def video_feed():
105
  session_id = request.args.get("id", "default")
106
  show_stats = request.args.get("show_stats", "1") == "1"
107
 
108
- if session_id not in [s.id for s in sessions]:
109
  session = Session(session_id)
110
- sessions.add(session)
111
  threading.Thread(target=frame_processor, args=[session_id], daemon=True).start()
112
  else:
113
- session = next(s for s in sessions if s.id == session_id)
114
  session.show_stats = show_stats
115
 
116
  return Response(stream_with_context(stream_mjpeg(session_id)), mimetype="multipart/x-mixed-replace; boundary=frame")
 
18
  app = Flask(__name__)
19
  PORT = int(os.environ.get("PORT", 5000))
20
 
21
+ sessions: dict[str, Session] = {}
22
 
23
  def frame_processor(session_id: str):
24
  global sessions
25
 
26
+ session: Session = sessions.get(session_id, None)
27
 
28
  if session is not None:
29
  cap = cv2.VideoCapture("classroom.mp4")
 
82
  def stream_mjpeg(session_id: str):
83
  global sessions
84
 
85
+ session: Session = sessions.get(session_id, None)
86
 
87
  if session is not None:
88
  while True:
 
105
  session_id = request.args.get("id", "default")
106
  show_stats = request.args.get("show_stats", "1") == "1"
107
 
108
+ if session_id not in sessions:
109
  session = Session(session_id)
110
+ sessions[session_id] = session
111
  threading.Thread(target=frame_processor, args=[session_id], daemon=True).start()
112
  else:
113
+ session = sessions[session_id]
114
  session.show_stats = show_stats
115
 
116
  return Response(stream_with_context(stream_mjpeg(session_id)), mimetype="multipart/x-mixed-replace; boundary=frame")