python-multiprocessing池卡住了

时间:2018-03-07 15:32:18

标签: python multiprocessing

我有以下代码(伪代码,因为原来太长了)

from multiprocessing import Pool

def _my_fun(params):
    out = # do someting
    return out

class myClass():
    def __init__(self,init_params):
       sef.init_params = init_params

    def process(self,some_list):
        data = []
        try:
            poll = Pool(processess=4)
            params = #some iterable object
            data = [pool.map(_my_func,params)
        except Exception as e:
            pool.close()
            pool.terminate()
            print(e)
        else:
            pool.terminate()
            pool.join()

         # other stuff

if __name__ == '__main__':
    inst1 = myClass(init_params1)
    data1 = inst1.process()
    # some other code

    inst2 = myClass(init_params2)
    data2 = inst2.process()
    # some other code

    inst3 = myClass(init_params3)
    data = inst3.process()
    # some other code

当我只将myClass实例化两次时,它工作得很好,但是我第三次调用它时,所有4个进程都被启动但是没有执行任何操作(%CPU使用的总是0.0):

u_id 26536  0.0 12.5 20983352 2047660 pts/8 S+  14:51   0:00 python my_script.py
u_id 26537  0.0 12.5 20983352 2047660 pts/8 S+  14:51   0:00 python my_script.py
u_id 26538  0.0 12.5 20983352 2047660 pts/8 S+  14:51   0:00 python my_script.py
u_id 26539  0.0 12.5 20983352 2047660 pts/8 S+  14:51   0:00 python my_script.py

有人知道为什么它适用于前两个,但不是第三个?

0 个答案:

没有答案