简体   繁体   中英

return value of multiprocessing and extract it as an input of another function in python

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)

and then I need

func2(results)

This is just a simple example of my problem. Please don't tell me to transfer a to numpy array first because my func2 is way more complicated than this example. Does anyone know how to do it please?

Thank you very much.

Maybe you are looking for something in this direction:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM