Python 在多处理程序中,100个子进程中只有2个获得CPU

Python 在多处理程序中,100个子进程中只有2个获得CPU,python,python-3.x,python-2.7,multiprocessing,fork,Python,Python 3.x,Python 2.7,Multiprocessing,Fork,面临的问题: 我有一个程序,它连接到服务器,发送一个哈希进行查找,并返回某种响应 在我当前的设置中,我有一个基本系统,它向服务器发送请求,每个请求都有1个。 因此,为了进行多个查找,我使用python中的多处理启动了100个进程来执行查找工作。过了一段时间,当我想知道我的子进程的行为时,我做了一个CPU和内存统计,它显示了一个有趣的行为,100个子进程中只有2个得到CPU和内存,其余的总是0.0 有人知道发生了什么吗?我的实现有问题 代码片段 def start_scan(files):

面临的问题:

我有一个程序,它连接到服务器,发送一个哈希进行查找,并返回某种响应

在我当前的设置中,我有一个基本系统,它向服务器发送请求,每个请求都有1个。 因此,为了进行多个查找,我使用python中的多处理启动了100个进程来执行查找工作。过了一段时间,当我想知道我的子进程的行为时,我做了一个CPU和内存统计,它显示了一个有趣的行为,100个子进程中只有2个得到CPU和内存,其余的总是0.0

有人知道发生了什么吗?我的实现有问题

代码片段

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

只是一个猜测,但是将open('result_clean.csv','a')作为f:的
是否会被阻塞,因为所有进程都试图附加到它?除了附加到文件之外,还有其他方法吗?我会先为每个进程写入不同的文件来确认这确实是个问题。如果真是这样,那么可能a)将结果传输回内存中的主进程(如果它相对较小)b)继续写入不同的文件并在稍后阶段聚合?
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