繁体   English   中英

subprocess.Popen的多个实例

[英]Multiple instances of subprocess.Popen

嗨,我刚刚开始使用python编程,我试图使用subprocess.Popen运行我使用“ make”编译的程序的多个实例。 但是在执行“ make”之前,我必须进行一些文本处理并生成“ make”将要使用的一组文件。 现在,我想同时运行具有不同生成文件的同一程序,并将该程序输出的所有实例的输出写入同一文件。 根据实例的数量,我还必须生成那么多文本文件。 本质上,我想同时执行第一个for循环以下的所有操作,让我们说'n'次。 提供的任何帮助将不胜感激:)。

for mC in range(monteCarlo):
    print "Simulation Number",str(mC+1),"of",str(monteCarlo)
    L = numpy.zeros((1,4),float)
    W = numpy.zeros((1,4),float)
    i = 0
    j = 0
    with open("1t.sp", "r") as inFile:
        with open("2t.sp","w") as outFile:
            line = inFile.readline()
            while (line != ""):
                newLine = []
                for words in line.split():
                    if words.startswith("W="):
                        W[0,i] = float(words[2:].replace('n',''))*random.normalvariate(1,widthDeviation)
                        #print i,words,str('W='+str(W[i]).strip('[]')+'n').replace(" ","")
                        words = str('W='+str(W[0,i]).strip('[]')+'n').replace(" ","")
                        i = i+1
                    elif words.startswith("L="):
                        L[0,j] = float(words[2:].replace('n',''))*random.normalvariate(1,lengthDeviation)
                        #print j,words,str('L='+str(L[j]).strip('[]')+'n').replace(" ","")
                        words = str('L='+str(L[0,j]).strip('[]')+'n').replace(" ","")
                        j = j+1
                    newLine.append(words)
            #print newLine
                outFile.write(" ".join(newLine))
                outFile.write("\n")
                line = inFile.readline()
    outFile.close()
    inFile.close()
    openWrite.write(str(W).strip('[]'))
    openWrite.write(str(L).strip('[]'))
    call(["make"])
    fRate = (open("tf.log","r").readlines()[34]).split()[-2]
    cSect = (open("tf.log","r").readlines()[35]).split()[-2]
    openWrite.write("\t")
    openWrite.write(fRate)
    openWrite.write(" ") 
    openWrite.write(cSect)
    openWrite.write("\n")
openWrite.close()   

如果您的系统具有多个处理器或内核,则可以通过使用多处理模块同时运行Python函数来利用它们:

import multiprocessing as mp

def run_mc(mC):
    print "Simulation Number", str(mC+1), "of", str(monteCarlo)
    ...
    call(["make"])
    fRate = (open("tf.log", "r").readlines()[34]).split()[-2]
    cSect = (open("tf.log", "r").readlines()[35]).split()[-2]
    return fRate, cSect

def log_result(result):
    # This is called whenever run_mc returns a result.
    # result is modified only by the main process, not the pool workers.
    fRate, cSect = result
    with open(..., 'a') as openWrite:
        openWrite.write('\t{f} {c}\n'.format(f = fRate, c = cSect))

def main():
    # mp.Pool creates a pool of worker processes. By default it creates as many
    # workers as the system has processors. When the problem is CPU-bound, there
    # is no point in making more.
    pool = mp.Pool()
    for mC in range(monteCarlo):
        # This will call run_mc(mC) in a worker process.
        pool.apply_async(run_mc, args = (mC), callback = log_result)

if __name__ == '__main__':
    main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM