1
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.

1 Answer 1

1

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())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.