Python 线程池映射中的多个数组
Multiple arrays in Python ThreadPool map
我正在编写一个 python 程序来为工人提供工作。我遇到 multiprocess.pool.ThreadPool
的问题,它只允许 1 个可迭代对象。我希望这两名工人的工作量相同(或几乎相同)。代码如下:
from multiprocessing.pool import ThreadPool
def f(worker,work):
print(f"{worker} | {work}")
workers = ["worker1","worker2"]
work = [1,2,3,4,5,6,7,8,9,10]
if __name__ == "__main__":
with ThreadPool(len(workers)) as p:
p.map(f,zip(workers,work))
预期产量(不需要工单):
worker1 | 1
worker2 | 2
worker1 | 4
worker2 | 3
...
如有任何帮助,我们将不胜感激。谢谢
我想你想要 ThreadPool.starmap
:
from multiprocessing.pool import ThreadPool
from itertools import cycle
def f(worker,work):
print(f"{worker} | {work}")
workers = ["worker1","worker2"]
work = [1,2,3,4,5,6,7,8,9,10]
if __name__ == "__main__":
with ThreadPool(len(workers)) as p:
p.starmap(f,zip(cycle(workers),work))
输出:
worker1 | 1
worker2 | 2
worker1 | 3
worker1 | 5
worker2 | 4
worker2 | 6
worker1 | 7
worker1 | 9
worker2 | 10
worker2 | 8
请注意,压缩工人时还需要使用 cycle()
,否则只会使用 work
列表中的前两个元素。
我正在编写一个 python 程序来为工人提供工作。我遇到 multiprocess.pool.ThreadPool
的问题,它只允许 1 个可迭代对象。我希望这两名工人的工作量相同(或几乎相同)。代码如下:
from multiprocessing.pool import ThreadPool
def f(worker,work):
print(f"{worker} | {work}")
workers = ["worker1","worker2"]
work = [1,2,3,4,5,6,7,8,9,10]
if __name__ == "__main__":
with ThreadPool(len(workers)) as p:
p.map(f,zip(workers,work))
预期产量(不需要工单):
worker1 | 1
worker2 | 2
worker1 | 4
worker2 | 3
...
如有任何帮助,我们将不胜感激。谢谢
我想你想要 ThreadPool.starmap
:
from multiprocessing.pool import ThreadPool
from itertools import cycle
def f(worker,work):
print(f"{worker} | {work}")
workers = ["worker1","worker2"]
work = [1,2,3,4,5,6,7,8,9,10]
if __name__ == "__main__":
with ThreadPool(len(workers)) as p:
p.starmap(f,zip(cycle(workers),work))
输出:
worker1 | 1
worker2 | 2
worker1 | 3
worker1 | 5
worker2 | 4
worker2 | 6
worker1 | 7
worker1 | 9
worker2 | 10
worker2 | 8
请注意,压缩工人时还需要使用 cycle()
,否则只会使用 work
列表中的前两个元素。