Illuminati0x5B
Illuminati0x5B

Reputation: 602

Python: passing Pool objects to a function in different process

What am I doing wrong in the following code?

import multiprocessing as mp
from multiprocessing import Process
import numpy as np

def fun(X):
    return X;

def funct(p,i):
    print 'Hey'
    res = [p.map(fun,range(2))];
    return res;


if __name__ == '__main__':
    pool = mp.Pool(2);
    output = mp.Queue();
    proc = [mp.Process(target = funct,args=(pool,i)) for i in range(2)]
    for p in proc:
        p.start()
    for p in proc:
        p.join()
    results = [output.get() for p in proc]
    print results

The program doesn't run after launching the process.

Upvotes: 4

Views: 1263

Answers (1)

dano
dano

Reputation: 94881

multiprocessing.Pool doesn't support being passed to child processes, as noted in the multiprocessing documentation:

Note that the methods of a pool should only ever be used by the process which created it.

So there actually is no way to do what you're trying. I'm assuming your code above is just an example to demonstrate the problem, so I won't comment on that directly, but you'll need to refactor your actual code to only call pool methods from the parent process (or possibly create the pool in the child itself, but that's probably not a good solution).

Upvotes: 4

Related Questions