Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python多处理池。如何获得所有结果_Python - Fatal编程技术网

python多处理池。如何获得所有结果

python多处理池。如何获得所有结果,python,Python,开始玩Python,遇到了一个愚蠢的情况。 我的函数返回两个值。使用MP我想让执行更快 import time import multiprocessing as mp def foo(bar, baz): print("start") time.sleep(2) print("end") return bar, baz if __name__ == '__main__': pool = mp.Pool(processes=4) ret =

开始玩Python,遇到了一个愚蠢的情况。 我的函数返回两个值。使用MP我想让执行更快

import time
import multiprocessing as mp

def foo(bar, baz):
    print("start")
    time.sleep(2)
    print("end")
    return bar, baz


if __name__ == '__main__':
    pool = mp.Pool(processes=4)

    ret = []
    i = 0
    while i < 4 :
        j = i + 1
        while j < 4 :
            results = [pool.apply_async(foo, args=(i, j))]
            j = j + 1
        i = i + 1

    output = [p.get() for p in results]
    print(output)

您在每次迭代中都会覆盖
结果
列表,但您应该将其附加到列表中。这应该可以做到:

results = []
while i < 4 :
    j = i + 1
    while j < 4 :
        results.append(pool.apply_async(foo, args=(i, j)))
        j = j + 1
    i = i + 1

output = [p.get() for p in results]
results=[]
而我<4:
j=i+1
当j<4时:
append(pool.apply_async(foo,args=(i,j)))
j=j+1
i=i+1
输出=[p.get()表示结果中的p]

诊断

以下是您的代码,其中包含一些调试,并使用
for
语句以提高可读性:

for i in range(4):
    for j in range(i+1, 4):
        results = [pool.apply_async(foo, args=(i, j))]
        print("TRACE", results]

output = [p.get() for p in results]
print("FINAL", output)
查看输出,您可以看到问题:尽管您创建了6个不同的结果收集器,
result
只是任何给定时间的最新结果。因此,您需要将它们全部收集起来

输出:

TRACE [<multiprocessing.pool.ApplyResult object at 0x7faf4b711390>]
TRACE [<multiprocessing.pool.ApplyResult object at 0x7faf4b7114e0>]
TRACE [<multiprocessing.pool.ApplyResult object at 0x7faf4b711588>]
TRACE [<multiprocessing.pool.ApplyResult object at 0x7faf4b711630>]
TRACE [<multiprocessing.pool.ApplyResult object at 0x7faf4b7116d8>]
start
TRACE [<multiprocessing.pool.ApplyResult object at 0x7faf4b711780>]
start
start
start
end
end
start
start
end
end
end
end
FINAL [(2, 3)]
TRACE [<multiprocessing.pool.ApplyResult object at 0x7f52af945390>, 
       <multiprocessing.pool.ApplyResult object at 0x7f52af945438>, 
       <multiprocessing.pool.ApplyResult object at 0x7f52af9454e0>, 
       <multiprocessing.pool.ApplyResult object at 0x7f52af945588>,  
       <multiprocessing.pool.ApplyResult object at 0x7f52af945630>, 
       <multiprocessing.pool.ApplyResult object at 0x7f52af9456d8>]
start
start
start
start
end
end
end
end
start
start
end
end
FINAL [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
输出:

TRACE [<multiprocessing.pool.ApplyResult object at 0x7faf4b711390>]
TRACE [<multiprocessing.pool.ApplyResult object at 0x7faf4b7114e0>]
TRACE [<multiprocessing.pool.ApplyResult object at 0x7faf4b711588>]
TRACE [<multiprocessing.pool.ApplyResult object at 0x7faf4b711630>]
TRACE [<multiprocessing.pool.ApplyResult object at 0x7faf4b7116d8>]
start
TRACE [<multiprocessing.pool.ApplyResult object at 0x7faf4b711780>]
start
start
start
end
end
start
start
end
end
end
end
FINAL [(2, 3)]
TRACE [<multiprocessing.pool.ApplyResult object at 0x7f52af945390>, 
       <multiprocessing.pool.ApplyResult object at 0x7f52af945438>, 
       <multiprocessing.pool.ApplyResult object at 0x7f52af9454e0>, 
       <multiprocessing.pool.ApplyResult object at 0x7f52af945588>,  
       <multiprocessing.pool.ApplyResult object at 0x7f52af945630>, 
       <multiprocessing.pool.ApplyResult object at 0x7f52af9456d8>]
start
start
start
start
end
end
end
end
start
start
end
end
FINAL [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
TRACE[,,
, 
, 
,  
, 
]
开始
开始
开始
开始
结束
结束
结束
结束
开始
开始
结束
结束
最后的[(0,1)、(0,2)、(0,3)、(1,2)、(1,3)、(2,3)]

您打印的唯一输出是最后一个,即退出循环时的
结果值。您是否正在尝试制作
结果
元组列表?