使用 ThreadPool Python 时的最大池大小

编程入门 行业动态 更新时间:2024-10-26 02:31:04
本文介绍了使用 ThreadPool Python 时的最大池大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在使用 ThreadPool 来实现多处理.使用多处理时,池大小限制应等于 CPU 内核数.我的问题 - 使用 ThreadPool 时,池大小限制应该是 CPU 内核数吗?

I am using ThreadPool to achieve multiprocessing. When using multiprocessing, pool size limit should be equivalent to number of CPU cores. My question- When using ThreadPool, should the pool size limit be number of CPU cores?

这是我的代码

from multiprocessing.pool import ThreadPool as Pool
class Subject():
    def __init__(self, url):
       #rest of the code
   def func1(self):
      #returns something
if __name__=="__main__":
   pool_size= 11
   pool= Pool(pool_size)
   objects= [Subject() for url in all_my_urls]
   for obj in objects:
     pool.apply_async(obj.func1, ())
   pool.close()
   pool.join()

最大池大小应该是多少?提前致谢.

What should be the maximum pool size be? Thanks in advance.

推荐答案

不能使用线程进行多处理,只能实现多线程.由于 GIL,多个线程不能在单个 Python 进程中同时运行,因此多线程仅在运行 IO 繁重的工作(例如与 Internet 交谈)并花费大量时间等待时才有用,而不是 CPU 繁重的工作(例如数学)不断占据核心.

You cannot use threads for multiprocessing, you can only achieve multithreading. Multiple threads cannot run concurrently in a single Python process because of the GIL and so multithreading is only useful if they are running IO heavy work (e.g. talking to the Internet) where they spend a lot of time waiting, rather than CPU heavy work (e.g. maths) which constantly occupies a core.

因此,如果您同时运行许多 IO 繁重的任务,那么拥有这么多线程将很有用,即使它超过 CPU 内核的数量.大量线程最终会对性能产生负面影响,但在您实际测量问题之前不要担心.大约 100 个线程应该没问题.

So if you have many IO heavy tasks running at once then having that many threads will be useful, even if it's more than the the number of CPU cores. A very large number threads will eventually have a negative impact on performance, but until you actually measure a problem don't worry. Something like 100 threads should be fine.

这篇关于使用 ThreadPool Python 时的最大池大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 16:36:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1396246.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大小   ThreadPool   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!