Python:部分清除RAM以保持脚本不断快速运行?

时间:2016-08-16 08:16:07

标签: python performance python-2.7 memory ram

我在Python中的基本代码迭代了2,000,000个传递给第三方fortran exe的参数组合。 Fortran对所需的输出进行建模,并创建一个大小为54KB的.csv文件。

一开始,我得到每秒100输出 - 在这个速度下,计算应该在20,000秒或5.5小时内完成(这很棒!)。不幸的是,每次迭代过程都会逐渐变慢。在10,000输出(0.5%)之后,我已经下降到每秒20个输出。在模型运行#30,000之后,它是每秒2个输出,当我在一天后查看我的计算机时,我运行#60,000,每秒运行少于1次 - 不用说我必须中止该过程。

现在我想知道导致计算速度如此大幅下降的原因。在关闭计算机并重新启动我最后一次杀死它的过程之后,它现在运行得和平时一样快 - 所以我认为它与某种内存溢出有关。

问题是:我可以以某种方式"冲洗"内存没有删除脚本中的信息(即我的参数组合矩阵和其他变量)或者我应该在30,000个模型运行后手动重启计算机?我也可以想到一个批处理文件,但我不擅长编码,所以我宁愿寻找一种正确的方法来用Python完成所有这些。

任何提示?谢谢!

编辑:好的,这是迭代的核心代码。在此之前,创建一个大小为10 x 2,000,000的矩阵作为查找表。这个矩阵随着时间的推移不变!

for run in range(n_total): # n_total is 2,000,000

    # build list for passing the parameters ("para_pro")
    para_pro = ['']*(13)
    para_pro[0:n_para] = para_grid[:,run]

    # add some stuff
    para_pro[-3] = SZA
    para_pro[-2] = OZA
    para_pro[-1] = rAA

    # conversion to string and add e-o-l charakcter
    para_pro = [str(para_pro[i])+'\n' for i in xrange(13)]

    # write to parameter-file
    with open(model_dir + parameter_file, 'w') as PFile:
        for line in para_pro:
            PFile.writelines(line)

    # generate file name (some internal labelling of the file which later tells me which combination I am looking at)
    filename = "run" + str(run+1) + "ID" + final_namelist[run]

    # create String for Batch-File
    batch = ['cd %s\n' % model_dir, 'D:\n', '(\n', 'echo %s\n' % filename, 'echo 2\n', 'echo 2\n', 'echo 2\n', 'echo 1\n', ') | fortran_model.exe\n']

    print "ID%i of %i" % (int(run+1), n_total)

    batch_id = run % 50 # creates numbers from 0 to 49 to cycle the used Batch-file if it is still open for read in the model

    # Write batch file
    with open(model_dir + "Batch_py%i.bat" % batch_id, 'w') as BFile:
        for line in batch:
            BFile.writelines(line)

    # Execute Batch -> execution of the model

    p = Popen(model_dir + "Batch_py%i.bat" % batch_id)
    stderr, stdout = p.communicate()

0 个答案:

没有答案