python线程-在迭代n个任务时始终有x个活动线程

python线程-在迭代n个任务时始终有x个活动线程,python,multithreading,list,queue,Python,Multithreading,List,Queue,我基本上想做的是: import threading import Queue def test_thread(elem, q): q.put(elem ** 2) a = [1,2,3,4,5,6,7,8] q = Queue.Queue() results = [] for x in range(8): print x threading.Thread(target=test_thread, args=(a[x], q)).start() results.

我基本上想做的是:

import threading
import Queue

def test_thread(elem, q):
    q.put(elem ** 2)

a = [1,2,3,4,5,6,7,8]
q = Queue.Queue()
results = []
for x in range(8):
    print x
    threading.Thread(target=test_thread, args=(a[x], q)).start()
    results.append(q.get())
但是我不想一次运行所有线程,我只想并行运行2,并在列表上迭代。完成一个线程后,应处理列表中的下一个值。我找不到一个例子,也不知道如何为此构造循环

此外,我不了解队列的行为。我本以为所有的平方数都在队列中。但实际上只有一个值?(上面的代码已更改为将所有结果存储在“结果”中)。提示,评论,关键字是高度赞赏

编辑:

第二个问题:


抱歉,我以为q.get()会返回所有结果。但它只是以类似队列的方式提供元素。

您可以使用线程池:

import threading
from multiprocessing.pool import ThreadPool

def test_thread(elem):
    return elem ** 2

a = [1,2,3,4,5,6,7,8]
pool = ThreadPool(2) # 2 worker threads
results = []
for x in range(8):
    print x
    results.append(pool.apply_async(test_thread, args=(a[x],)))

results = [result.get() for result in results]
# You can also replace this for loop altogether using pool.map
# and get the same result:
# results = pool.map(test_thread, range(8))
print(results)
输出:

0
1
2
3
4
5
6
7
[1, 4, 9, 16, 25, 36, 49, 64]
ThreadPool
类是模块中大部分未记录的部分。它也可以通过访问。它允许您创建线程池来处理任意数量的工作项,同时始终将并发处理的工作项数量限制为您指定的数量。您可以使用normal的文档来了解其API。它完全一样,除了它所说的“进程”之外,你用“线程”代替它


我不确定我是否遵循了你问题的第二部分,关于
Queue.Queue
。在for循环的每次迭代中,您将一个项目放入
队列
测试线程
,然后使用
结果在for循环内使用它。append(q.get())
。因此,虽然
队列中一次最多只有一个项目,但它被用来传输
结果列表中的所有值-范围(8)
列表中的每个项目一个。

您可以使用线程池:

import threading
from multiprocessing.pool import ThreadPool

def test_thread(elem):
    return elem ** 2

a = [1,2,3,4,5,6,7,8]
pool = ThreadPool(2) # 2 worker threads
results = []
for x in range(8):
    print x
    results.append(pool.apply_async(test_thread, args=(a[x],)))

results = [result.get() for result in results]
# You can also replace this for loop altogether using pool.map
# and get the same result:
# results = pool.map(test_thread, range(8))
print(results)
输出:

0
1
2
3
4
5
6
7
[1, 4, 9, 16, 25, 36, 49, 64]
ThreadPool
类是模块中大部分未记录的部分。它也可以通过访问。它允许您创建线程池来处理任意数量的工作项,同时始终将并发处理的工作项数量限制为您指定的数量。您可以使用normal的文档来了解其API。它完全一样,除了它所说的“进程”之外,你用“线程”代替它


我不确定我是否遵循了你问题的第二部分,关于
Queue.Queue
。在for循环的每次迭代中,您将一个项目放入
队列
测试线程
,然后使用
结果在for循环内使用它。append(q.get())
。因此,虽然
队列中一次最多只有一个项目,但它被用来传输
结果列表中的所有值-范围(8)
列表中的每个项目一个。

您可以使用线程池:

import threading
from multiprocessing.pool import ThreadPool

def test_thread(elem):
    return elem ** 2

a = [1,2,3,4,5,6,7,8]
pool = ThreadPool(2) # 2 worker threads
results = []
for x in range(8):
    print x
    results.append(pool.apply_async(test_thread, args=(a[x],)))

results = [result.get() for result in results]
# You can also replace this for loop altogether using pool.map
# and get the same result:
# results = pool.map(test_thread, range(8))
print(results)
输出:

0
1
2
3
4
5
6
7
[1, 4, 9, 16, 25, 36, 49, 64]
ThreadPool
类是模块中大部分未记录的部分。它也可以通过访问。它允许您创建线程池来处理任意数量的工作项,同时始终将并发处理的工作项数量限制为您指定的数量。您可以使用normal的文档来了解其API。它完全一样,除了它所说的“进程”之外,你用“线程”代替它


我不确定我是否遵循了你问题的第二部分,关于
Queue.Queue
。在for循环的每次迭代中,您将一个项目放入
队列
测试线程
,然后使用
结果在for循环内使用它。append(q.get())
。因此,虽然
队列中一次最多只有一个项目,但它被用来传输
结果列表中的所有值-范围(8)
列表中的每个项目一个。

您可以使用线程池:

import threading
from multiprocessing.pool import ThreadPool

def test_thread(elem):
    return elem ** 2

a = [1,2,3,4,5,6,7,8]
pool = ThreadPool(2) # 2 worker threads
results = []
for x in range(8):
    print x
    results.append(pool.apply_async(test_thread, args=(a[x],)))

results = [result.get() for result in results]
# You can also replace this for loop altogether using pool.map
# and get the same result:
# results = pool.map(test_thread, range(8))
print(results)
输出:

0
1
2
3
4
5
6
7
[1, 4, 9, 16, 25, 36, 49, 64]
ThreadPool
类是模块中大部分未记录的部分。它也可以通过访问。它允许您创建线程池来处理任意数量的工作项,同时始终将并发处理的工作项数量限制为您指定的数量。您可以使用normal的文档来了解其API。它完全一样,除了它所说的“进程”之外,你用“线程”代替它


我不确定我是否遵循了你问题的第二部分,关于
Queue.Queue
。在for循环的每次迭代中,您将一个项目放入
队列
测试线程
,然后使用
结果在for循环内使用它。append(q.get())
。因此,虽然
队列中一次最多只能有一个项目,但它被用来传输
结果列表中的所有值-范围(8)
列表中的每个项目一个。

干杯!这两个版本都适合你吗?我在for循环版本中得到错误“test_thread()参数在*之后必须是序列,而不是int”。然而,pool.map\u async()函数可以工作。@KamSen抱歉,我在
apply\u async
调用中有一个输入错误;
args
参数必须采用一个iterable,比如一个单元素元组(
(a[x],)
)。我原来忘了后面的逗号。现在修好了。我也应该使用
map
而不是
map\u async
,因为我们希望在继续之前等待所有结果准备就绪。干杯!这两个版本都适合你吗?我在for循环版本中得到错误“test_thread()参数在*之后必须是序列,而不是int”。然而,pool.map\u async()函数可以工作。@KamSen抱歉,我在
apply\u async
调用中有一个输入错误;
args
参数必须采用一个iterable,比如一个单元素元组(
(a[x],)
)。我原来忘了后面的逗号。现在修好了。我也应该使用
map<