Python多处理:自定义进程池
问题描述:
我将Process类继承到一个类中,我称之为EdgeRenderer。我想使用multiprocessing.Pool
,除了常规进程之外,我希望它们是我的EdgeRenderer的实例。可能?怎么样?Python多处理:自定义进程池
答
从杰西洛勒尔:
目前还不支持 API中,但不会是一个坏加成。 我会看看添加它来 python2.7/2.6.3 3.1本周
答
我在API中看不到任何钩子。您可能能够通过使用initializer
和initargs
参数来复制所需的功能。或者,你可以建立功能分为可调用的对象,您使用的映射:
class EdgeRenderTask(object):
def op1(self,*args):
...
def op2(self,*args):
...
p = Pool(processes = 10)
e = EdgeRenderTask()
p.apply_async(e.op1,arg_list)
p.map(e.op2,arg_list)
答
这似乎工作:
import multiprocessing as mp
ctx = mp.get_context() # get the default context
class MyProcess(ctx.Process):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
print("Hi, I'm custom a process")
ctx.Process = MyProcess # override the context's Process
def worker(x):
print(x**2)
p = ctx.Pool(4)
nums = range(10)
p.map(worker, nums)
你们是不是要这样写代码中使用多线程? – 2009-04-11 23:27:40
多处理。 – 2009-04-12 10:04:26