我有一个python生成器,它从视频中输出cv2图像(帧(,并使用concurrent.futures.ThreadPoolExecutor
进行处理。我看到tqdm一直挂着,直到发电机耗尽。但是,如果我从gen_frames_dummy
向executor提供信息,tqdm会随着每个任务的完成而更新。
如有任何协助,我们将不胜感激。
def gen_frames(s):
for frame_num in frames_2_get:
# skip to frame in video
s.set(cv2.CAP_PROP_POS_FRAMES, frame_num-1)
res, frame = s.read()
if res:
yield frame
def gen_frames_dummy(s):
for frame_num in frames_2_get:
yield np.zeros((16,16,3))
def frame_op(f):
# process the frame
pass
with ThreadPoolExecutor() as executor:
frames_g = gen_frames(vid_capture)
list(tqdm(executor.map(frame_op, frames_g), total=len(frames2get))
map
等待,直到所有工作程序完成,然后将结果传递给代码中的tqdm
。
我想你可以这样重写,使tqdm
按预期工作。
for args in tqdm(frames_g, total=len(frames_2_get)):
executor.submit(frame_op, args)