对python使用多处理时Cpu被锁定

对python使用多处理时Cpu被锁定,python,join,multiprocessing,cpu-usage,Python,Join,Multiprocessing,Cpu Usage,我的代码: def create_rods(folder="./", kappas=10, allowed_kappa_error=.3, radius_correction_ratio=0.1): """ Create one rod for each rod_data and for each file returns [RodGroup1, RodGroup2, ...] """ names, files = impo

我的代码:

def create_rods(folder="./", kappas=10, allowed_kappa_error=.3,
                radius_correction_ratio=0.1):
    """
    Create one rod for each rod_data and for each file
    returns [RodGroup1, RodGroup2, ...]
    """
    names, files = import_files(folder=folder)
    if len(files) == 0:
        print "No files to import."
        raise ValueError
    states = [None for dummy_ in range(len(files))]
    processes = []
    states_queue = mp.Queue()
    for index in range(len(files)):
        process = mp.Process(target=create_rods_process,
                        args=(kappas, allowed_kappa_error,
                        radius_correction_ratio, names,
                        files, index, states_queue))
        processes.append(process)
    run_processes(processes)        #This part seem to take a lot of time.
    try:
        while True:
            [index, state] = states_queue.get(False)
            states[index] = state
    except Queue.Empty:
        pass    
    return names, states

def create_rods_process(kappas, allowed_kappa_error,
                    radius_correction_ratio, names,
                    files, index, states_queue):
    """
    Process of method.
    """
    state = SystemState(kappas, allowed_kappa_error,
                radius_correction_ratio, names[index])
    data = import_data(files[index])
    for dataline in data:
        parameters = tuple(dataline)
        new_rod = Rod(parameters)
        state.put_rod(new_rod)
    state.check_rods()
    states_queue.put([index, state])

def run_processes(processes, time_out=None):
    """
        Runs all processes using all cores.
    """
    running = []
    cpus = mp.cpu_count()
    try:
        while True:
        #for cpu in range(cpus):
            next_process = processes.pop()
            running.append(next_process)
            next_process.start()
    except IndexError:
        pass
    if not time_out:
        try:
            while True:
                for process in running:
                    if not process.is_alive():
                        running.remove(process)
        except TypeError:
            pass
    else:
        for process in running:
            process.join(time_out)
我希望流程结束,但我的流程受阻。我不知道run_processs()方法或create_()方法是否有问题。使用join时,CPU被释放,但程序无法继续运行。

来自Python

加入使用队列的进程

请记住,将项目放入队列的进程将在终止之前等待,直到“feeder”线程将所有缓冲项目馈送到底层管道。(子进程可以调用队列的Queue.cancel\u join\u线程方法以避免此行为。)

这意味着,无论何时使用队列,都需要确保在加入流程之前,已放入队列的所有项目最终都将被删除。否则,您无法确保将项目放入队列的进程将终止。还要记住,非守护进程将自动加入


在耗尽队列之前加入进程会导致死锁。在加入进程之前,您需要确保队列已清空。

我得到除一个进程外的所有进程的futex\u wait\u queue\u me,该列中显示0,cpu使用率为100%。我使用f(x)=x**2函数尝试了我的run\u processs函数,但也被阻止,因此似乎是run\u进程错误。我更改了try,除非使用if not len(跑步):休息,我仍然有问题,但是f(x)=x**2一切正常。问题与队列以及队列的填充/清空方式有关。我想我的队列放置超时有错误。我不想丢失我在队列中放置的内容,但我不知道为什么要花费这么长时间。在不进行多处理的情况下运行时,需要花费合理的时间。那么,我必须如何使用队列?这是吗如果需要帮助,请修复格式并使代码更具可读性。