_name_ == "_main_" 多进程在windows和linux下的差异
windows报错 linux成功
import multiprocessing as mp
def ParameterCalculation(list,output):
output.put(1)
result = 0
if __name__ == "__main__":
output = mp.Queue()
processes = []
processes.append(mp.Process(target=ParameterCalculation, args=([], output)))
processes.append(mp.Process(target=ParameterCalculation, args=([], output)))
for p in processes:
p.start()
resultMp = [output.get() for p in processes]
for p in processes:
p.join()
for j in range(0, len(resultMp)):
result = result + resultMp[j]
FreYield = round((1212 / float(result)) * 100, 4)
print(FreYield)
windows成功 linux成功
import multiprocessing as mp
def ParameterCalculation(list,output):
output.put(1)
result = 0
if __name__ == "__main__":
output = mp.Queue()
processes = []
processes.append(mp.Process(target=ParameterCalculation, args=([], output)))
processes.append(mp.Process(target=ParameterCalculation, args=([], output)))
for p in processes:
p.start()
resultMp = [output.get() for p in processes]
for p in processes:
p.join()
for j in range(0, len(resultMp)):
result = result + resultMp[j]
FreYield = round((1212 / float(result)) * 100, 4)
print(FreYield)
windows的多进程和Linux的多进程的创建形式不同,windows的多进程通过导入模块实现,不在main函数内的代码会被在子进程中再次执行,而linux的多线程是通过fork来创建子进程,可以按照代码顺序执行下去