Python多处理:自定义进程池

时间:2009-04-11 21:53:56

标签: python multiprocessing pool

我将Process类子类化为一个我称之为EdgeRenderer的类。我想使用multiprocessing.Pool,除了常规进程,我希望它们是我的EdgeRenderer的实例。可能?怎么样?

3 个答案:

答案 0 :(得分:3)

来自Jesse Noller:

  

目前不支持   API,但不会是一个坏的补充。   我会考虑添加它   python2.7 / 2.6.3 3.1本周

答案 1 :(得分:2)

我没有在API中看到它的任何钩子。您可以通过使用initializerinitargs参数来复制所需的功能。或者,您可以将功能构建到用于映射的可调用对象中:

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)

答案 2 :(得分:2)

这似乎有效:

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)
相关问题