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")
posted @ 2022-04-03 17:02  Philbert  阅读(202)  评论(0编辑  收藏  举报