subprocess.Popen 的多个实例

发布于 2024-12-22 02:59:36 字数 1996 浏览 4 评论 0原文

你好,我刚刚开始用 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()   

Hi I have just started programming in python and I am trying to use subprocess.Popen to run multiple instances of a program that i compile using "make". But before i do a "make", I have to do some text processing and generate a set of files that "make" will use. Now I would like to run the same program with different generated files simultaneously and write the output of all the instances of the output of the program into the same file. Depending upon the number of instances, I will also have to generate that many text files. In essence, I want to do all operations below the first for loop, simultaneously, lets say 'n' times. Any help offered would be greatly appreciated :).

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()   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

破晓 2024-12-29 02:59:36

如果您的系统有多个处理器或内核,您可以通过使用 多处理模块来利用它 同时运行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()

If your system has multiple processors or cores you can take advantage of that by using the multiprocessing module to run Python functions concurrently:

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文