简体   繁体   English

ThreadPoolExecutor 线程数

[英]ThreadPoolExecutor number of threads

I am trying to use the ThreadPoolExecutor in Python using the futures backport package.我正在尝试使用 futures backport 包在 Python 中使用 ThreadPoolExecutor。 However, the problem is that all threads are executed at the same time so there is no actual pooling take place.然而,问题是所有线程都是同时执行的,因此没有发生实际的池化。 More specifically I get 10 threads of that function instead of 5 and then the others.更具体地说,我得到了该函数的 10 个线程,而不是 5 个,然后是其他线程。 I use the following code do you find something wrong or it is just the backported implementation?我使用以下代码您是否发现有问题,或者它只是向后移植的实现? Thank you!谢谢!

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())

In the documentation it says that it's typical to have multiple threads per worker:在文档中,它说每个工人有多个线程是典型的:

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

Changed in version 3.5: If max_workers is None or not given, it will default to the number of processors on the machine, multiplied by 5, assuming that ThreadPoolExecutor is often used to overlap I/O instead of CPU work and the number of workers should be higher than the number of workers for ProcessPoolExecutor. 3.5 版更改:如果 max_workers 为 None 或未给出,则默认为机器上的处理器数量,乘以 5,假设经常使用 ThreadPoolExecutor 来重叠 I/O 而不是 CPU 工作,并且工作人员的数量应该高于 ProcessPoolExecutor 的工作线程数。

Probably it doesn't default to 5 multiplier because you're using a version other than 3.5+ or for some other internal optimization reason.可能它不会默认为 5 乘数,因为您使用的是 3.5+ 以外的版本或出于其他一些内部优化原因。

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

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