'''
如果要启动大量的子进程,可以用进程池的方式批量创建子进程:
'''
def test_task(name):
print 'Run task %s (%s)...' % (name, os.getpid())
start = time.time()
time.sleep(random.random() * 3)
end = time.time()
print 'Task %s runs %0.2f seconds.' % (name, (end - start))
if __name__=='__main__':
print 'Parent process %s.' % os.getpid()
p = Pool()
for i in range(5):
p.apply_async(test_task, args=(i,))
print 'Waiting for all subprocesses done...'
p.close()
p.join()
print 'finish'
对Pool
对象调用join()
方法会等待所有子进程执行完毕,调用join()
之前必须先调用close()
,调用close()
之后就不能继续添加新的Process
了。
'''
以Queue为例,在父进程中创建两个子进程,一个往Queue里写数据,一个从Queue里读数据:
'''
#写数据
def write(queue):
for i in range(1,10):
print 'put %d to queue.'% i
queue.put(i)
time.sleep(random.random())
#读数据
def read(queue):
while True:
i = queue.get(True)
print 'get %d from queue.'%i
#测试代码
def test():
q = Queue()#创建队列,并传给子进程
WriteProcessor = Process(target = write,args=(q,))
ReadProcessor = Process(target = read,args=(q,))
#启动写进程,写入数据
WriteProcessor.start()
#启动读进程,读取数据
ReadProcessor.start()
#等待WriteProcessor结束
WriteProcessor.join()
#终止读进程
ReadProcessor.terminate()
参考资料:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868323401155ceb3db1e2044f80b974b469eb06cb43000