摆脱僵尸进程

时间:2013-07-24 22:42:40

标签: linux python-3.x subprocess zombie-process

我无法摆脱一些僵尸进程。我已经阅读了这个问题的其他一些答案,并且从我收集的内容中,当您的子进程未正确关闭时会发生这种情况。在我向代码中添加while循环之前,我没遇到这个问题。看一看。

def worker(self):
    cmd = ["/home/orlando/CountMem","400000000","2000"]
    p = subprocess.Popen(cmd,stdout=subprocess.PIPE)
    id_list = []
    id_list.append(p.pid)



    while len(id_list) > 0:
         for num in id_list:
             stat_file = open("/proc/{0}/status".format(num))
             mem_dict = {}
             for i, line in enumerate(stat_file):
                 if i == 3:
                    #print line
                     mem_dict['ID'] = line
                     print(mem_dict)
                 if i == 10:
                    #print line
                     mem_dict['Mem'] = line
                     print(mem_dict)

    return id_list

if __name__ == '__main__':
    count = multiprocessing.cpu_count()
    pool = multiprocessing.Pool(processes = count)
    print(pool.map(worker,['ls']*count))

我的代码循环遍历每个子进程的“/ proc / PID / status”多次获取信息。如果没有“while”循环,它不会产生僵尸进程,但它也不能满足我想要它做的事情。通过循环,它可以满足我的要求,但它也会产生僵尸进程。我的问题是如何让我的代码不会产生僵尸。以下是我得到的一些输出:

{'ID': 'Pid:\t2446\n'}                                                                                              
{'ID': 'Pid:\t2441\n'}                                                                                              
{'Mem': 'VmPeak:\t  936824 kB\n', 'ID': 'Pid:\t2446\n'}                                                             
{'Mem': 'VmPeak:\t  542360 kB\n', 'ID': 'Pid:\t2441\n'}                                                             
{'ID': 'Pid:\t2442\n'}                                                                                              
{'Mem': 'VmPeak:\t 1037580 kB\n', 'ID': 

这一直持续到子进程完成后立即开始打印:

{'ID': 'Pid:\t2602\n'}                                                                                              
{'ID': 'Pid:\t2607\n'}                                                                                              
{'ID': 'Pid:\t2606\n'}                                                                                              
{'ID': 'Pid:\t2604\n'}                                                                                              
{'ID': 'Pid:\t2605\n'}                                                                                              
{'Mem': 'Threads:\t1\n', 'ID': 'Pid:\t2606\n'}                                                                      
{'Mem': 'Threads:\t1\n', 'ID': 'Pid:\t2607\n'}                                                                      
{'Mem': 'Threads:\t1\n', 'ID': 'Pid:\t2605\n'}                                                                      
{'Mem': 'Threads:\t1\n', 'ID': 'Pid:\t2604\n'} 

任何人都可以帮助我理解和解决正在发生的事情吗?

1 个答案:

答案 0 :(得分:0)

我想出了我需要添加p.poll()的答案我在while循环中添加了它。

def worker(self):
    cmd = ["/home/orlando/CountMem","400000000","2000"]
    p = subprocess.Popen(cmd,stdout=subprocess.PIPE)
    id_list = []
    id_list.append(p.pid)



    while len(id_list) > 0:
         for num in id_list:
             stat_file = open("/proc/{0}/status".format(num))
             mem_dict = {}
             for i, line in enumerate(stat_file):
                 if i == 3:
                    #print line
                     mem_dict['ID'] = line
                     print(mem_dict)
                 if i == 10:
                    #print line
                     mem_dict['Mem'] = line
                     print(mem_dict)
         p.poll()
    return id_list

if __name__ == '__main__':
    count = multiprocessing.cpu_count()
    pool = multiprocessing.Pool(processes = count)
    print(pool.map(worker,['ls']*count))