多线程运行变慢

目的:监控某些进程一段时间内性能使用情况 例如待监控进程名为:a.exe,b.exe,c.exe,d.exe

本人编写的方案如下,但执行速度只有预期的一半,怎么样才能恢复实际运行速度, 在monitor_process_cpu_memory_usage改用多进程没问题,但是每起一次进程就会初始化环境一次 代码如下:

def monitor_process_cpu_memory_usage(action=None,processes=None,timeout=60,batch_num=1000):
    "监控入口"
    procs = deque()
    logging.info('monitor_process_cpu_memory_usage start time:%d' % time.time())
    for procname in processes:
        future_time = time.time() + timeout
        p = threading.Thread(target=__name_to_many,args=(procname,future_time,batch_num,))
        procs.append(p)

    for thread in procs:
        thread.start()

    if hasattr(action,'__call__'):
        logging.info(u'[*] execute action:%s' % action.__name__)
        action()

    for thread in procs:
        thread.join()

    logging.info('monitor_process_cpu_memory_usage end time:%d' % time.time())


def __name_to_many(procname=None,future_timeout=60,batch_num=1000):
    "当存在多个同名进程时,创建并发同时监控"
    global lock
    exists_pid,threads = deque(),deque()
    logging.info('__name_to_many start time:%d' % time.time())
    while True:
        if time.time() > future_timeout: break
        # 把进程名转成pid
        for proc in get_process_object_with_pid_pname(procname):
            thread_local.proc = proc
            pid = thread_local.proc.pid
            pname = thread_local.proc.name()
            if pid in exists_pid: continue
            else: exists_pid.append(pid)

            cpu_filename = os.path.join(result_dir,u'info_cpu_%s_%d.txt' % (pname,pid))
            p2 = threading.Thread(target=get_process_info_with_cpu_rate,args=(thread_local.proc,cpu_filename,future_timeout,batch_num,))
            threads.extend([p2])
            logging.info('__name_to_many for middle time:%d' % time.time())
            logging.info('exists_pid length:%d,threads length:%d,[pname:%s,pid:%d]' % (len(exists_pid),len(threads),pname,pid))

        for thread in threads:
            thread.setDaemon(True)
            thread.start()
        threads.clear()

    logging.info('__name_to_many end time:%d' % time.time())

def get_process_info_with_cpu_rate(proc=None,filename=u'',future_timeout=60,batch_num=1000):
    "获取cpu信息,并存到本地txt"
    global lock
    thread_local.proc = proc
    thread_local.data_list = deque()
    cpu_threshold_value = int(read_xml('cpu_threshold',conf_xml))
    t1 = time.time()
    try:
        while True:
            if time.time() > future_timeout: break
            lock.acquire()
            if system.cpu_percent() >= cpu_threshold_value:
                thread_local.data_list.extend(generate_data(thread_local.proc))
            lock.release()
            if len(thread_local.data_list) % 100000 ==0:
                write_data1(filename,thread_local.data_list)

        if len(thread_local.data_list):
            write_data1(filename,thread_local.data_list)
    except Exception,e:
        try:
            lock.release()
        except RuntimeError,e:
            pass
if __name__ == '__main__':
    clear_environment()
    monitor_process_cpu_memory_usage(run_notepad,['a.exe','b.exe','c.exe','d.exe'])
1 comments