python并行程序
python并行程序
下面的脚本给出并行程序示例,只需要把最大的循环拆分成若干份,给每份循环分配一个CPU即可。
'''
step-01: insert the function into def main
step-02: set the variable noprocess based on the CPUs
step-03: run
'''
from multiprocessing import Process
from time import time, sleep
import numpy as np
def main(str1):
# divided the loop into some parts
no = int(str1)
nbeg = (no-1)*10000
nend = no*10000
# loop begining
name = "out_"+str1+".tmp"
f = open(name,"w")
for i in np.arange(nbeg,nend):
if (i%1000==0): sleep(0.5)
f.write(i)
f.close
if __name__ == '__main__':
tstart = time()
threads = []
noprocess = 10 # setting the Number of CPUs
for i in range(1,1+noprocess):
name = '{:02d}'.format(i)
tpro = Process(target=main,args=(name,))
threads.append(tpro)
print("Part NO="+name+"begining")
tpro.start()
sleep(1.0)
# parent process run...
print("Multiprocessing join begining")
# after end running
for t in threads:
t.join()
tend =time()
print("Sum up cost",(tend-tstart)/60,"min")
本文来自博客园,作者:Philbert,转载请注明原文链接:https://www.cnblogs.com/liangxuran/p/16096427.html