0

I intend to be able to play video via opencv while some other process runs in the background. My current working looks like this

import cv2
async def function1():
    cap = cv2.VideoCapture('video.mp4')
    while  True:
        ret, frame = cap.read()
        if ret == True:
            cv2.imshow('Frame', frame)
            if cv2.waitKey(60) & 0xFF == ord('q'):
                break
        else:
            cap.release()
            cap = cv2.VideoCapture('video.mp4')
            ret, frame = cap.read()

async def function2():
    while True:
        print("Inside Function 2")

async def main():
    f1=loop.create_task(function1())
    f2 =loop.create_task(function2())
    await asyncio.gather(f1,f2)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
asyncio.run(main())

expecting that while my video continuously plays, function 2 will also continuously run (in this example printing "inside function 2"). But with this script only the video plays and function 2 is never invoked: how can I run both function 1 and function 2 simultaneously so that while my video is playing function 2 also continuously prints?

0

1 Answer 1

0

Try Threading instead of async/await so you can have multiple threads(subprograms) running at the same time

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.