How to break while loop in task

Hello,
I want to write a Task which records a video from a webcam using opencv. Actually I’m not sure if it is a good idea to try this in airflow.

I dont want to set a timer for the loop, is it possible to break the while loop by using X-COM or Variable?

I want to use the REST API and a button to start and stop the recording.

cap = cv2.VideoCapture(0)

writer = cv2.VideoWriter(...)

while True:
    ret, frame = cap.read()
    writer.write(frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break


cap.release()
writer.release()
cv2.destroyAllWindows()

Is it worth a try? Should this be done by trying change the boolean or by “simulating the Keyboard press “q”” to escape the loop.

Please let me know if I should provide further information on the problem.

Thanks in advance
Franz