我遇到了一个奇怪的问题asyncio.Queue- 不是在项目可用时立即返回,而是等待队列已满,然后再返回任何内容。我意识到在使用队列来存储从 收集的帧时cv2.VideoCapture,maxsize队列越大,在屏幕上显示任何内容所需的时间就越长,然后,它看起来像是收集到队列中的所有帧的序列。
这是一个功能,一个错误,还是我只是用错了?
无论如何,这是我的代码
import asyncio
import cv2
import numpy as np
async def collecting_loop(queue):
print("cl")
cap = cv2.VideoCapture(0)
while True:
_, img = cap.read()
await queue.put(img)
async def processing_loop(queue):
print("pl")
await asyncio.sleep(0.1)
while True:
img = await queue.get()
cv2.imshow('img', img)
cv2.waitKey(5)
async def main(e_loop):
print("running main")
queue = asyncio.Queue(loop=e_loop, maxsize=10)
await asyncio.gather(collecting_loop(queue), processing_loop(queue))
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main(e_loop=loop))
except KeyboardInterrupt:
pass
finally:
loop.close()
Run Code Online (Sandbox Code Playgroud)