Multiprocessing Pool inside Process time out

此生再无相见时 提交于 2019-12-06 03:00:22

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()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!