Python多处理仅使用最多50%的CPU

时间:2014-07-11 20:42:44

标签: python multiprocessing

我试图在CPU的所有内核之间划分任务。我这样做基本上就像下面的代码一样。例如,我有一个有80个元素的网格,我的CPU中有8个核心。对于网格的每个元素,我需要进行计算,对于这种情况,每个核心将进行10次计算。代码正在运行,但它没有我想要的开发,CPU I的最大使用率为50%,但大部分时间它保持在25-30%。如何让这段代码运行得更快?如何更多地使用CPU?

from multiprocessing import Process, cpu_count

def initialize(info):
    my_object = my_class(info)
    grid = my_object.get_grid()
    core_amount = cpu_count()
    var_aux, start_index, end_index = 0, 0, 0
    processes = []
    while var_aux <= core_amount:
        # these if/elif is just to determinate the start and end index of the calculation for each core job
        if var_aux < core_amount:
            relation = floor(len(grid)/core_amount)
            start_index = relation * var_aux
            end_index = relation * (var_aux + 1)
        elif var_aux == core_amount:
            start_index = floor(len(grid)/core_amount) * var_aux
            end_index = len(grid)
        if start_index != end_index:
          processes.append(Process(target = partial_calculation, args=(my_object, grid, start_index, end_index )))
          processes[var_aux].start()
        var_aux += 1
    for process in processes:
        process.join()
    return my_object

def partial_calculation(my_class_object, grid, start_index, end_index): #this is the method that is called in parallel
    while start_index < end_index:
        my_class_object.make_calculation(grid[start_index]) #Here is the calculation of a grid state
        start_index += 1

if __name__ == '__main__':
    info = something()
    my_object = initialize(info)
    my_object.use_it_after_calculation_is_done()

我希望我很清楚,谢谢!

0 个答案:

没有答案