import multiprocessing with open(args.AS, 'r') as File: data = File.readlines() ProcessNum = int(args.p) average = int(len(data)/ProcessNum) p = multiprocessing.Pool(16) for processId in range(0, ProcessNum): if processId == ProcessNum-1: start = processId*average end = len(data) else: start = processId*average end = (processId+1)*average out.append(p.apply_async(mulProcessPSI, (data[start:end], processId+1))) p.close() p.join()
获取进程结果
out数组中存着每个进程计算的结果
get()对每个进程调用get方法获得结果
1 2 3 4
with open(args.o, 'w') as File: for result in out: for item in result.get(): File.write(item)