max_workers解释

使用多线程函数ThreadPoolExecutor(),如果使用默认参数,则max_workers =None,参考源代码发现

if max_workers is None:
     # Use this number because ThreadPoolExecutor is often
     # used to overlap I/O instead of CPU work.
     max_workers = (os.cpu_count() or 1) * 5

进一步:
max_workers = (os.cpu_count() or 1) * 5 的解释
但是为什么是乘以 5 呢?

os.cpu_count()
Return the number of CPUs in the system; return None if indeterminable.
# 返回系统中CPU的数目;如果不确定,则返回无。
    print(os.cpu_count())  # 64 
    # 与多线程ThreadPoolExecutor 不同
    # 实际上,多进程ProcessPoolExecutor(max_workers=60)最多能用60
    # 本地查找设备管理器,处理器只有40个,没找到具体原因。
    看这里https://bugs.python.org/issue26903
    ProcessPoolExecutor(max_workers=64) crashes on Windows
    我是max_workers=61就crashes了,使用ProcessPoolExecutor默认参数(max_workers=None)也是同样的错误。查ProcessPoolExecutor源码,
            if max_workers is None:
                  self._max_workers = os.cpu_count() or 1
    说明max_workers 实际上是64,也就是说61到64都会报如下错误。

    Exception in thread QueueManagerThread:
    ...
    ValueError: need at most 63 handles, got a sequence of length 63
    
    print(max_workers)  # 320
    # 输出因个人电脑而异
    print((None or 1) * 5)  # 5
    print((0 or 1) * 5)  # 5
    print((2 or 1) * 5)  # 10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值