繁体   English   中英

为什么 ThreadPool 会导致超过一个核心的 CPU 使用率?

[英]Why would ThreadPool cause more than one core worth of CPU usage?

我有如下代码:

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 秒。
  • 40年代是我开始剧本的地方
  • 不容易看到,但有16个核心。

另一个注意事项:我没有将 n_workers 设置为 16,因为我有 16 个内核。 只是巧合。

那么为什么这会一次使用 75% 的 16 个内核呢?

因为如果可以的话,您的线程池将使用每个线程 1 个核心。 这就是提供最大并行性和最大化吞吐量的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM