简体   繁体   English

Python多处理:自定义进程池

[英]Python multiprocessing: Pool of custom Processes

I am subclassing the Process class, into a class I call EdgeRenderer. 我将Process类子类化为一个我称之为EdgeRenderer的类。 I want to use multiprocessing.Pool , except instead of regular Processes, I want them to be instances of my EdgeRenderer. 我想使用multiprocessing.Pool ,除了代替常规进程,我希望它们是我的EdgeRenderer的实例。 Possible? 可能? How? 怎么样?

From Jesse Noller: 来自Jesse Noller:

It is not currently supported in the API, but would not be a bad addition. 它目前不支持API,但不会是一个糟糕的补充。 I'll look at adding it to python2.7/2.6.3 3.1 this week 我将在本周将其添加到python2.7 / 2.6.3 3.1

This seems to work: 这似乎有效:

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)

I don't see any hook for it in the API. 我在API中没有看到任何钩子。 You might be able to get away with replicating your desired functionality by using initializer and initargs argument. 您可以通过使用initializerinitargs参数来复制所需的功能。 Alternately, you can build the functionality into the callable object that you use for mapping: 或者,您可以将功能构建到用于映射的可调用对象中:

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