繁体   English   中英

Python多处理:自定义进程池

[英]Python multiprocessing: Pool of custom Processes

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

来自Jesse Noller:

它目前不支持API,但不会是一个糟糕的补充。 我将在本周将其添加到python2.7 / 2.6.3 3.1

这似乎有效:

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)

我在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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM