#coding=utf-8from multiprocessing import Poolimport os, time, randomdef run_task(name): print 'Task %s (pid = %s) is running...' % (name, os.getpid()) time.sleep(random.random() * 5) print 'Task %s end.' % nameif __name__ == '__main__': print 'Current process %s.' % os.getpid() p = Pool(processes = 3) for i in range(10): p.apply_async(run_task, i) print 'Waiting for all subprocesses done...' p.close() p.join() print "All subprocesses done."#join()方法会等待所有的进程执行完毕, 调用join()之前必须先调用close()#调用close()之后,进程池便无法再添加新的进程