ThreadPoolExecutor 线程数

问题描述 投票:0回答:1

我正在尝试使用 futures backport 包在 Python 中使用 ThreadPoolExecutor。然而,问题是所有线程都是同时执行的,因此没有发生实际的池化。更具体地说,我得到了该函数的 10 个线程,而不是 5 个,然后是其他线程。我使用以下代码,您是否发现有问题,或者它只是向后移植的实现? 谢谢!

with ThreadPoolExecutor(max_workers=5) as executor:
    futures = [executor.submit(f, X, y) for t in range(10)]
    for future in as_completed(futures):
        self.trees.append(future.result())
python multithreading thread-safety threadpoolexecutor concurrent.futures
1个回答
0
投票

在文档中,它说每个工作人员通常有多个线程:

https://docs.python.org/3/library/concurrent.futures.html

3.5 版更改:如果 max_workers 为 None 或未给定,则默认为机器上的处理器数量乘以 5,假设 ThreadPoolExecutor 经常用于重叠 I/O 而不是 CPU 工作和工作人员数量应高于 ProcessPoolExecutor 的工作人员数量。

它可能不会默认为 5 乘数,因为您使用的是 3.5+ 以外的版本或出于某些其他内部优化原因。

© www.soinside.com 2019 - 2024. All rights reserved.