2009-04-11 116 views
5

我將Process類繼承到一個類中,我稱之爲EdgeRenderer。我想使用multiprocessing.Pool,除了常規進程之外,我希望它們是我的EdgeRenderer的實例。可能?怎麼樣?Python多處理:自定義進程池

+0

你們是不是要這樣寫代碼中使用多線程? – 2009-04-11 23:27:40

+0

多處理。 – 2009-04-12 10:04:26

回答

3

從傑西洛勒爾:

目前還不支持 API中,但不會是一個壞加成。 我會看看添加它來 python2.7/2.6.3 3.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

這似乎工作:

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)