Python创建多进程,用Queue传递信息
创建10个进程,分别计算0到1000各个区间的和,主进程汇总
__author__ = 'Administrator' # -*- coding:utf-8 -*- ''' @author:qiushui ''' import os import threading import multiprocessing import Queue def calculator(q, calContext): sumCal = 0; for i in range(calContext[0], calContext[1],1): sumCal += i; q.put(sumCal) lst = range(0,1000,1) i = 0 q = multiprocessing.Queue(); if __name__ == "__main__": while i <= 1000: p = multiprocessing.Process(target=calculator, args=(q,(i,(i+1)*100))) p.start() p.join() i += 100; result = 0 while not q.empty(): element = q.get() result+=element print("result = " + repr(result))