gpt4 book ai didi

python - 如何累积 pool.apply_async 调用的结果?

转载 作者:行者123 更新时间:2023-12-01 07:06:41 30 4
gpt4 key购买 nike

我想调用 pool.apply_async(func) 并在结果可用时立即累积结果,而无需相互等待。


import multiprocessing
import numpy as np

chrNames=['chr1','chr2','chr3']
sims=[1,2,3]



def accumulate_chrBased_simBased_result(chrBased_simBased_result,accumulatedSignalArray,accumulatedCountArray):
signalArray = chrBased_simBased_result[0]
countArray = chrBased_simBased_result[1]

accumulatedSignalArray += signalArray
accumulatedCountArray += countArray


def func(chrName,simNum):
print('%s %d' %(chrName,simNum))

result=[]
signal_array=np.full((10000,), simNum, dtype=float)
count_array = np.full((10000,), simNum, dtype=int)
result.append(signal_array)
result.append(count_array)

return result


if __name__ == '__main__':

accumulatedSignalArray = np.zeros((10000,), dtype=float)
accumulatedCountArray = np.zeros((10000,), dtype=int)

numofProcesses = multiprocessing.cpu_count()
pool = multiprocessing.Pool(numofProcesses)

for chrName in chrNames:
for simNum in sims:
result= pool.apply_async(func, (chrName,simNum,))
accumulate_chrBased_simBased_result(result.get(),accumulatedSignalArray,accumulatedCountArray)

pool.close()
pool.join()

print(accumulatedSignalArray)
print(accumulatedCountArray)



这样,每个 pool.apply_async 调用都会等待其他调用结束。有没有办法摆脱这种互相等待的情况?

最佳答案

您在每次迭代中使用 result.get(),并使主进程等待函数准备就绪。

请在下面找到一个工作版本,打印显示当“func”准备好时完成累积,并添加随机 sleep 以确保相当大的执行时间差异。

import multiprocessing
import numpy as np
from time import time, sleep
from random import random

chrNames=['chr1','chr2','chr3']
sims=[1,2,3]



def accumulate_chrBased_simBased_result(chrBased_simBased_result,accumulatedSignalArray,accumulatedCountArray):
signalArray = chrBased_simBased_result[0]
countArray = chrBased_simBased_result[1]

accumulatedSignalArray += signalArray
accumulatedCountArray += countArray


def func(chrName,simNum):

result=[]
sleep(random()*5)
signal_array=np.full((10000,), simNum, dtype=float)
count_array = np.full((10000,), simNum, dtype=int)
result.append(signal_array)
result.append(count_array)
print('%s %d' %(chrName,simNum))

return result


if __name__ == '__main__':

accumulatedSignalArray = np.zeros((10000,), dtype=float)
accumulatedCountArray = np.zeros((10000,), dtype=int)

numofProcesses = multiprocessing.cpu_count()
pool = multiprocessing.Pool(numofProcesses)

results = []
for chrName in chrNames:
for simNum in sims:
results.append(pool.apply_async(func, (chrName,simNum,)))

for i in results:
print(i)

while results:
for r in results[:]:
if r.ready():
print('{} is ready'.format(r))
accumulate_chrBased_simBased_result(r.get(),accumulatedSignalArray,accumulatedCountArray)
results.remove(r)

pool.close()
pool.join()

print(accumulatedSignalArray)
print(accumulatedCountArray)

关于python - 如何累积 pool.apply_async 调用的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58418335/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com