100个子进程中只有2个在多进程程序中获取CPU

时间:2018-05-20 17:01:28

标签: python python-3.x python-2.7 multiprocessing fork

面临的问题:

我有一个连接到服务器的程序,并发送一个哈希值进行查找并返回某种respose。

在我目前的设置中,我有一个基本系统,每个服务器向服务器发送1个请求。 因此,为了进行多次查找,我在python中使用了多处理来完成查找工作的100个进程。当我想知道我的子进程是如何行为的时候做了一个CPU和内存统计,显示了有趣的行为只有100个孩子中有2个得到CPU和记忆,其余的总是0.0。

任何有想法的人都会发生什么事?我的实施有问题。

CODE SNIPPET

def start_scan(files):
        """
        Multiprocessing scanner
        """
        try:
                for filedata in files:
                        start = time.time()
                        res = scan(os.path.join('/home/something/test/'+ filedata))
                        with open('result_clean.csv' , 'a') as f:
                                data_in_file = "Result {} for file {}".format(str(res),filedata)
                                f.write(data_in_file)
                                f.write('\n')

                        end = time.time()
                        logger.info('File {} took {} for processing'.format(filedata,end-start))
        except Exception as e:
                logger.error('Error in scanning files {}'.format(str(e)))

def clean_(self,worker=100):
    pool = mp.Pool(processes=worker)
    start = time.time()
    input_files = []
    file_path = self.folder_clean_scan
    input_files.append(os.listdir(file_path))
    targetLists = []
    count = 0
    for i in range(0, 1):
            if (i < ( len(input_files) % 1)):
            # This one gets a remainder
                    targetLists.append(input_files[count:(count + 1 + ( len(input_files) / 1))])
                    count += 1 + ( len(input_files) / 1)
            else:
            # No remainder
                    targetLists.append(input_files[count:(count + ( len(input_files) / 1))])
                    count += len(input_files) / 1
    #print targetLists[0]
    results = [ pool.apply_async(start_scan,
            args=(x)) \
            for x in targetLists ]

    try:

            output = [ p.get() for p in results ]
            print output
    except:
            traceback.print_exc()
    end = time.time()
    print end -start

内存状况信息

ps -ax -o %cpu,%mem,cmd | grep app
 0.8  0.0 python app.py
 3.3  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py
 0.0  0.0 python app.py

0 个答案:

没有答案