将任务分配给每个单独的核心

时间:2015-08-14 02:43:39

标签: python multiprocessing python-multiprocessing

代码:

import multiprocessing,pdb

def do_calculation(data):
    print"check %d"%(data)
    return data * 2

def start_process():
    print 'Starting', multiprocessing.current_process().name

if __name__ == '__main__':
    inputs = list(range(10)) 
    pool_size = multiprocessing.cpu_count()
    pool = multiprocessing.Pool(processes=pool_size,
                                initializer=start_process,)
    pool_outputs= pool.map(do_calculation, inputs)
    pool.close() # no more tasks
    pool.join()  # wrap up current tasks
    print 'Pool    :', pool_outputs

输出:

Starting PoolWorker-1
check 0
check 1
check 2
check 3
check 4
check 5
check 6
check 7
check 8
check 9
Starting PoolWorker-2
Starting PoolWorker-3
Starting PoolWorker-4
Starting PoolWorker-5
Starting PoolWorker-6
Starting PoolWorker-7
Starting PoolWorker-8
Pool    : [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

我想在每个核心上运行CPU密集型任务,每个核心都执行一个实例。在上面的例子中,我发现只有核心才能完成所有的工作。 (我也关心池中的输出顺序。)

我做错了什么或弄错了I / O输出?

0 个答案:

没有答案