并行处理中的迭代

时间:2019-09-07 00:09:09

标签: python parallel-processing iteration itertools

我想我很亲密,但是我想不出正确的方法来迭代输入。这是我的代码的示例:

from multiprocessing import Pool
from functools import partial
from itertools import product

def f(n, x, y):
    return print("{} {} {}".format(n, x, y))

n = 3

c = [[1,2,3],[4,5,6]]
d = [[7,8,9],[10,11,12]]


if __name__ == '__main__':
    p = Pool(3)
    func = partial(f, n)
    p.starmap(func, product(zip(c,d), repeat=2))

这是结果:

3 ([1, 2, 3], [7, 8, 9]) ([1, 2, 3], [7, 8, 9])
3 ([1, 2, 3], [7, 8, 9]) ([4, 5, 6], [10, 11, 12])
3 ([4, 5, 6], [10, 11, 12]) ([1, 2, 3], [7, 8, 9])
3 ([4, 5, 6], [10, 11, 12]) ([4, 5, 6], [10, 11, 12])

但是我想要的是

3 ([1, 2, 3], [7, 8, 9]) 
3 ([1, 2, 3], [10, 11, 12])
3 ([4, 5, 6], [7, 8, 9])
3 ([4, 5, 6], [10, 11, 12])

1 个答案:

答案 0 :(得分:2)

您的预期输出只是cd的乘积,因此没有理由zip或重复。

更改:

p.starmap(func, product(zip(c,d), repeat=2))

收件人:

p.starmap(func, product(c, d))

演示:https://repl.it/repls/GoodWorthwhileLeadership