views:

160

answers:

1

When ever I use the following code the pool result always returns a timeout, is there something logically incorrect I am doing?

from multiprocessing import Pool, Process, cpu_count

def add(num):
  return num+1

def add_wrap(num):
  new_num = ppool.apply_async(add, [num])
  print new_num.get(timeout=3)

ppool = Pool(processes=cpu_count() )

test = Process(target=add_wrap, args=(5,)).start()

I'm aware of this bug, and would have thought that it would have been fixed in python 2.6.4?

+1  A: 

You can't pass Pool objects between processes.

If you try this code, Python will raise a exception : 'NotImplementedError: pool objects cannot be passed between processes or pickled'.

from multiprocessing import Queue, Pool

q = Queue()
ppool = Pool(processes=2)                                                       
q.put([ppool])
ppool = q.get()

So if you want your code to work, just create your Pool object in the add_wrap method.

from multiprocessing import Pool, Process, cpu_count

def add(num):
  return num+1

def add_wrap(num):
  ppool = Pool(processes=cpu_count() )
  new_num = ppool.apply_async(add, [num])
  print new_num.get(timeout=3)

test = Process(target=add_wrap, args=(5,)).start()
Gautier Hayoun
I was thinking along the same lines, but it still results in a Timeout exception. Looks like Pool() was never meant to be called from inside Process(), although I would have thought it would have been a feasible operation.
NeonNinja
I don't get a Timeout exception with python 2.6.4 in Ubuntu Karmic. What Operating System/Python version do you use?
Gautier Hayoun
My bad tested your solution using an python 2.6 which fails, works fine on 2.6.4, +accepted
NeonNinja
Glad to hear that.
Gautier Hayoun