from multiprocessing import Pool
a=[1,2,3,4,5,6]
def func1(a):
return a**2
def func2(a):
x= np.zeros(1)
for i in a:
x += i
return x
if __name__ == "__main__":
pool = Pool( os.cpu_count())
results = pool.map(func1, a)
print(results)
然后我需要
func2(results)
这只是我问题的一个简单例子。请不要告诉我先将数组转移到numpy,因为我的func2比这个例子复杂得多。有人知道怎么做吗?
非常感谢。
也许你正在寻找这个方向的东西:
import multiprocessing as mp
import numpy as np
def func1(n):
return n ** 2
def func2(a):
pool = mp.Pool(mp.cpu_count())
results = pool.map(func1, a)
pool.close()
pool.join()
print(results)
# [1, 4, 9, 16, 25, 36]
x = np.sum(results)
return x
if __name__ == "__main__":
a = [1, 2, 3, 4, 5, 6]
out = func2(a)
print(out)
# 91