python - 并行处理中的迭代

标签 python parallel-processing iteration python-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])

最佳答案

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

更改:

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

至:

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

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

关于python - 并行处理中的迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57829705/

相关文章:

Python 简单循环并行化 Jupyter Notebook

sql-server - 通过SSIS分区导入海量数据

python - 为什么我不能在类方法中使用 python 模块 concurrent.futures?

python - 迭代时出现索引错误

python - 升级OpenCV brew 公式: Python not loading properly

python - 如何在 Python 中使用线程?

python - 如何让 virtualenv 更喜欢它的本地库而不是全局库

c# - 如何使多个 API 调用更快?

Python将递归排列函数转换为迭代函数

swift - 如何在异步迭代中设置DispatchGroup?