【发布时间】:2021-04-26 18:37:26
【问题描述】:
我的代码如下所示:
def get_image_stats(fp):
img = cv2.imread(fp)
return img.shape[0], img.shape[1], img.shape[0]/img.shape[1]
with ThreadPool(16) as pool:
res = list(tqdm(pool.imap_unordered(get_image_stats, df.file_path), total=len(df)))
heights, widths, ars = list(zip(*res))
唯一特定于库的部分是 cv2.imread,它只是将图像文件加载到 numpy 数组中,因此它是 I/O 绑定的。
为什么我的 CPU 使用率会这样?
关于该图像的注释:
- 横轴 i 时间以秒为单位,纵轴是 cpu % 使用率,范围从 0% 到 100%。更新间隔为 1 秒。
- 40s 是我开始脚本的地方
- 不容易看到,但有16个核心。
另一个注意事项:我没有将 n_workers 设置为 16,因为我有 16 个内核。只是巧合。
那么为什么会同时使用 75% 的 16 个内核?
【问题讨论】: