【发布时间】:2016-05-11 16:02:15
【问题描述】:
如何在以下代码中将打开线程的最大值限制为 20?我知道过去曾有人问过一些类似的问题,但我特别想知道如何最好地使用队列和工作示例(如果可能的话)。
# b is a list with 10000 items
threads = [threading.Thread(target=targetFunction, args=(ptf,anotherarg)) for ptf in b]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
【问题讨论】:
-
你试过
b[:20]吗?或者你想用最多 20 个线程处理整个范围?你的问题不太清楚。 -
# b 是一个包含 10000 个项目的列表,不能更改
-
所以使用线程池并让
targetFunction从队列中拉取工作? -
例如,您希望每个循环处理 20 个线程吗?还是什么?
-
targetFunction 正在从服务器下载信息,我一次不能打开超过 20 个连接。这就是为什么我想将打开线程的最大值限制为 20
标签: python multithreading