I'm creating a real-time event detector. It reads a gaming stream and detects some events there like goals, timer updates and etc. All of the events should be sent via websocket, so I use python websockets and asyncio libraries. Code is the following:
main_stack = []
cap = cv2.VideoCapture("vid_2.mp4")
game = Game(main_stack)
async def handler(websocket):
while True:
ret, frame = cap.read()
await asyncio.sleep(0.05)
game.run_detectors(frame)
while main_stack:
elem = main_stack.pop()
await websocket.send(elem.get_json())
async def main():
async with websockets.serve(handler, "", 8765):
await asyncio.Future()
if __name__ == "__main__":
asyncio.run(main())
Line game.run_detectors(frame) adds events to main stack if detected. Then if there are some events in the stack, they're sent via websocket. The problem is that when no client is connected, program freezes, missing events from translation which runs in real time. How can I change the code, so that the stack is updated independently of websocket. And websocket constantly checks updates and sends events if they're present in the stack?