【发布时间】:2018-09-30 15:22:28
【问题描述】:
我正在尝试使用python的线程池
concurrent.futures.ThreadPoolExecutor
我正在进行 api 调用,我想使用线程池执行器对其进行并行化
异步运行 api 调用 1 秒
executor = ThreadPoolExecutor(max_workers=10)
initial_start_time = time.time()
count = 0
while True:
start_time = time.time()
future = executor.submit(api_call_func)
end_time = time.time()
if ((end_time-initial_start_time) > 1):
break;
count = count+1
list.append(future)
print(count)
现在,当我将 max_workers 中定义的工人数量从 1 更改为 10 时。计数开始减少。
1 - 10K
5 - 1K
10 - 608
因此,使用更多工人实际上会损害性能。为什么会这样?我知道 python 一次只使用一个线程。但由于 api 调用是 IO 绑定的,这应该会有所帮助。
单个 rpc api 调用大约需要 5 毫秒。这就像没有任何处理的最低限度的 API。
谁能告诉我这里发生了什么?
【问题讨论】:
标签: python concurrency threadpool threadpoolexecutor