G-09
G-09

Reputation: 449

return value of multiprocessing , 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? I tried something like this, but it didn't work.

from itertools import repeat
from multiprocessing import Pool
import os
import numpy as np
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

def func22():
    if __name__ == "__main__":
        pool = Pool( os.cpu_count())
        results = pool.map(func1, a)
        x= func2(results)
    return x

print(func22())

Thank you very much.

Upvotes: 1

Views: 90

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54812

You just had the nesting a bit wrong. Remember that, because of the way multiprocessing has to reload your module in every new process, everything except one-time initialization must be inside the if __name__ protection.

This works.

from itertools import repeat
from multiprocessing import Pool
import os
import numpy as np

def func1(a):
    return a**2

def func2(a):
    x= np.zeros(1)
    for i in a:
        x += i
    return x

def func22():
    pool = Pool( os.cpu_count())
    results = pool.map(func1, a)
    x= func2(results)
    return x

if __name__ == "__main__":
    a=[1,2,3,4,5,6]
    print(func22())

Upvotes: 1

Related Questions