如何将itertools.product与多处理协调?

时间:2018-01-17 14:00:19

标签: python multiprocessing itertools

我在https://stackoverflow.com/a/19609168/2867928分享了代码。我正在尝试使用多处理。我用过这样的改造:

count=1   
def foo(i):

    global count
    dobre2=[]

    if tuple(reversed(i)) >= tuple(i):           
       a = ['v{}'.format(x%2) for x in i] 
       # the rest of the code

    return dodane_pary 

if __name__ == '__main__':

    pool = mul.Pool(4)
    result = pool.map(foo, itertools.product(tuple(range(2)), repeat=4))

    pool.close()
    pool.join()

    print (result)

相反:

count=1
dobre2=[]

for i in itertools.product(tuple(range(2)), repeat=4): 
    if tuple(reversed(i)) >= tuple(i):
       a = ['v{}'.format(x%2) for x in i]
       # the rest of the code

预期产出:

[[0, 2, 4, 6], [0, 2, 4, 1], [0, 2, 3, 8], [0, 2, 3, 5], [2, 3, 8, 7], [6, 1, 3, 8], [2, 3, 5, 9], [11, 0, 2, 3], [11, 4, 1, 5], [7, 1, 5, 9]]

不幸的是,我没有收到预期的退出,但是这个表格的列表是:

[[[0, 2, 4, 6]], [[0, 2, 4, 6], [0, 2, 4, 1]], [[0, 2, 1, 4]], [[0, 2, 4, 6], [0, 2, 4, 1], [0, 2, 3, 5]], [[0, 2, 1, 4]], [[0, 2, 1, 4], [2, 1, 4, 3]], [[0, 2, 4, 6], [0, 2, 4, 1], [0, 2, 3, 5], [0, 7, 9, 8]], [[0, 2, 1, 4], [2, 1, 4, 3], [0, 5, 7, 3]], [[0, 2, 1, 4], [2, 1, 4, 3], [0, 5, 7, 3]], [[0, 2, 1, 4], [2, 1, 4, 3], [0, 5, 7, 3], [5, 0, 2, 1]], [[0, 2, 1, 4], [2, 1, 4, 3], [0, 5, 7, 3], [5, 0, 2, 1]], [[0, 2, 1, 4], [2, 1, 4, 3], [0, 5, 7, 3], [5, 0, 2, 1], [1, 4, 3, 7]], [[0, 2, 1, 4], [2, 1, 4, 3], [0, 5, 7, 3], [5, 0, 2, 1], [1, 4, 3, 7]], [[0, 2, 1, 4], [2, 1, 4, 3], [0, 5, 7, 3], [5, 0, 2, 1], [1, 4, 3, 7]], [[0, 2, 1, 4], [2, 1, 4, 3], [0, 5, 7, 3], [5, 0, 2, 1], [1, 4, 3, 7]], [[0, 2, 1, 4], [2, 1, 4, 3], [0, 5, 7, 3], [5, 0, 2, 1], [1, 4, 3, 7], [3, 7, 5, 9]]]

此外,每次启动程序时,我都会得到一些更改结果,就像进程是随机分配的一样。我究竟做错了什么?如何很好地同步?

0 个答案:

没有答案