I have written a python code, for feeding multiple cameras using opencv and websockets ( not socket io ).
The first segment of the code consists of a function that handles the camera inputs and sending it to the websocket reciever side.
The third function which runs in main via asyncio.run is a fucntion that uses ThreadPoolExecutor to connect to a single ip but different ports. The idea is that for different camera feeds i communicate to different ports.
Now, THE PROBLEM here is that, the code works works fine for the first 30 seconds, but after that it takes nearly 14-15 seconds delay to RESTART the thread and back to feeding, In other words there is 14 seconds freeze in the middle of every run and ive used normal threading library but the issue remains same. What could be the solution to this? and what can be done instead? Thanks in advance
The first segment of the code consists of a function that handles the camera inputs and sending it to the websocket reciever side.
async def server_handler(websocket, path, camera_index): cap = cv2.VideoCapture(camera_index) while cap.isOpened(): try: ret, frame = cap.read() if not ret: break frame = cv2.resize(frame, (400, 300)) _, buffer = cv2.imencode('.jpg', frame, [int(cv2.IMWRITE_JPEG_QUALITY), 50]) frame_data = base64.b64encode(buffer).decode('utf-8') await websocket.send(frame_data) except websockets.exceptions.ConnectionClosed: print(f"Connection closed for camera {camera_index}.") break cap.release()The second segment is running the server with asyncio event loop that runs forever.
`def run_server(camera_index, port): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) start_server = websockets.serve( lambda websocket, path, ci=camera_index: server_handler(websocket, path, ci), config.IP, port ) loop.run_until_complete(start_server) loop.run_forever()
`The third function which runs in main via asyncio.run is a fucntion that uses ThreadPoolExecutor to connect to a single ip but different ports. The idea is that for different camera feeds i communicate to different ports.
`async def main(): with ThreadPoolExecutor() as executor: futures = [executor.submit(run_server, camera_index, port) for camera_index, port in zip(config.CAMERA_INDICES, config.PORTS)] await asyncio.gather(*futures)
`Now, THE PROBLEM here is that, the code works works fine for the first 30 seconds, but after that it takes nearly 14-15 seconds delay to RESTART the thread and back to feeding, In other words there is 14 seconds freeze in the middle of every run and ive used normal threading library but the issue remains same. What could be the solution to this? and what can be done instead? Thanks in advance

Gribouillis write May-05-2024, 10:07 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.