我正在将Process类继承为一个我称为EdgeRenderer的类。我想使用multiprocessing.Pool,除非我希望它们成为我的EdgeRenderer的实例,而不是常规进程。有可能吗?多么?
发布于 2009-04-12 14:24:47
来自杰西·诺勒的话:
接口目前不支持它,但这将是一个不错的补充。我将在本周将其添加到python2.7/2.6.3 3.1中
发布于 2016-12-02 02:50:07
这似乎起作用了:
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-12 01:26:07
我在API中看不到它的任何挂钩。你也许可以通过使用initializer和initargs参数来复制你想要的功能。或者,您可以将该功能构建到用于映射的callable对象中:
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)https://stackoverflow.com/questions/740844
复制相似问题