_name_ == "_main_" 多进程在windows和linux下的差异

_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来创建子进程,可以按照代码顺序执行下去

 

上一篇:python selenium 关闭命令窗口时,自动关闭浏览器


下一篇:react 环境下安装 storybook