python并发请求接口_关于 py 的线程池,并发请求接口

本文介绍了Python的ThreadPoolExecutor类,用于并发请求接口。默认的线程池大小基于CPU核心数的5倍设定,因为多线程在Python中并不利用多核,过多的线程切换会导致性能损失。建议参考Google和Stack Overflow讨论,了解最佳线程池大小的设置。
摘要由CSDN通过智能技术生成

1

2019-10-23 15:02:01 +08:00

class ThreadPoolExecutor(_base.Executor):

# Used to assign unique thread names when thread_name_prefix is not supplied.

_counter = itertools.count().__next__

def __init__(self, max_workers=None, thread_name_prefix='',

initializer=None, initargs=()):

"""Initializes a new ThreadPoolExecutor instance.

Args:

max_workers: The maximum number of threads that can be used to

execute the given calls.

thread_name_prefix: An optional name prefix to give our threads.

initializer: An callable used to initialize worker threads.

initargs: A tuple of arguments to pass to the initializer.

"""

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

这里源码里都写了建议线程池大小了

对 Python 来说, 多线程并不会利用多核, 所以一堆线程是靠系统的不断切换来确定线程完成状态的, 切换的多了自然开销就大了, 性能损失也就大了

多进程也是一个道理, 你就俩核心的话, 不断切换来切换去, 那 CPU 大部分时间都在切换状态上, 根本没时间干活, 也会变慢

权威一点的搜 Google , 一个意思

https://www.google.com.hk/search?q=Python+best+thread+pool+size

https://stackoverflow.com/questions/42541893/maximum-pool-size-when-using-threadpool-python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值