开发者

Multiprocessing Pool inside Process time out

开发者 https://www.devze.com 2022-12-11 10:17 出处:网络
When ever I use the following code the pool result always returns a timeout, is there something logicall开发者_如何学编程y incorrect I am doing?

When ever I use the following code the pool result always returns a timeout, is there something logicall开发者_如何学编程y 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?


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()
0

精彩评论

暂无评论...
验证码 换一张
取 消