Python调用其他可执行程序

  1. Linux系统: 为了nohup命令可以用
  2. 已经安装python, g\+\+, 以及python里需要的包
  1. 起因:
    1. 本学期正在修读陈焱老师热统课, 正在经受先痛后快的洗礼
    2. 我有一个叫做nano.cpp的文件, 写这个代码用了我半个学期
    3. 我要靠它在三天里cram up一篇PRB, 这个屎山有很多个参数
  2. 动机:
    1. 但是我不想一天到晚坐在我的电脑前, 等着前一个程序执行完, 然后手动开启后一个程序
    2. 我也不想写不同参数的并行, 并行需要重构代码, 我懒
  3. 结果:
    1. 我用了两天半的时间写Python控制C程序运行, 剩下6h.
    2. 物理楼HPC用3h跑完所有参数, 得到结果文件, 剩下3h.
      1. 在此期间我用时112min学会了LaTeX的基础操作:
      2. 我去OverLeaf上找到了文章模板, 并且仔细阅读
      3. 我学会了MatplotLib的操作
    3. 我用MatplotLib绘制出了全部输出参数的图像, 用时30min, 剩下2h30min
      1. 一共 128 * 12 * 5 = 7680张图, 并且选出其中有用的
    4. 我开始写LaTeX, 写完文章用时1h30min, 剩下1h
    5. 我去装模做样引用参考温馨啊, 用时30min, 剩下30min
    6. 成败在此一举! 伟大的LaTeX让我只用了29min30s就完成了论文最终拍板! 感谢上帝, 我还有30s时间将论文上传到elearning上!
  4. 以上纯属瞎说, 下面正文…
def checknanoProcess():
    nownanoProcess = 0
    pids = psutil.pids()
    for pid in pids:
        try:
            p = psutil.Process(pid)
            if (p.name() == "nano"):
                nownanoProcess += 1
        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
            print("Process Ended Ahead of checkPID " + str(pid) + ". SKIP!")
    return nownanoProcess
// 获取相关参数
particleR = atof(argv[1]);
T = atof(argv[2]);
J = atof(argv[3]);
kc = atof(argv[4]);
ks = atof(argv[5]);
// H.x[0] = atof(argv[6]);
// H.x[1] = atof(argv[7]);
// H.x[2] = atof(argv[8]);
# 只需要py文件和cpp文件在同一文件夹下就可以了
import os
import sys
import time
import psutil
# import numpy
from multiprocessing import cpu_count
# from decimal import Decimal
 
 
# 保证存在文件(夹): [nanodata, nanolog]
def checkFolderFile():
    # 保证存在文件夹: [nanodata, nanolog]
    need = [
        False, False, False,
        "nanodata", "nanolog", "nano"]
    needlen = int(len(need) / 2)  # 方便后来iter
    myDir = os.listdir(os.getcwd())  # 当前目录
 
    for d in myDir:
        for i in range(needlen):
            if (d == need[i+needlen]):
                need[i] = True
 
    for i in range(needlen-1):
        if not(need[i]):
            print("dir ./" + need[i+needlen] + " not found. mkdiring...")
            os.popen("mkdir " + need[i+needlen])
            time.sleep(0.5)
        else:
            print("dir ./" + need[i+needlen] + " existed.")
 
    i = needlen-1
    if not(need[i]):
        print("execute file ./" + need[i+needlen] + " not found. Compiling...")
        os.popen("g++ nano.cpp -o nano -O3")
        time.sleep(10)  # 需要足够长的时间来编译
    else:
        print("execute file ./" + need[i+needlen] + " existed.")
 
    return
 
 
# 生成cmd句子
def genCmd(particleR, T, J, kc, ks):
    n = 8  # 用于截断不必要的项目
    temp = "./nanolog/R" + str(particleR)[0:n] + "T" + str(T)[0:n]\
        + "J" + str(J)[0:n] + "kc" + str(kc)[0:n] + "ks" + str(ks)[0:n]\
        + ".log"
    myCmd = "nohup ./nano " + str(particleR)[0:n] + " " + str(T)[0:n]\
        + " " + str(J)[0:n] + " " + str(kc)[0:n] + " " + str(ks)[0:n]\
        + " > " + temp + " 2>&1 &"
    # myCmd = "nohup ./nano " + str(particleR) + " " + str(T) + " " + str(J) + " " + str(kc) + " " + str(ks)
    return myCmd
 
 
# 检查当前nano进程有几个
def checknanoProcess():
    nownanoProcess = 0
    pids = psutil.pids()
    for pid in pids:
        try:
            p = psutil.Process(pid)
            if (p.name() == "nano"):
                nownanoProcess += 1
        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
            print("Process Ended Ahead of checkPID " + str(pid) + ". SKIP!")
    return nownanoProcess
 
 
if __name__ == '__main__':
    # 更改默认路径为当前文件所在目录
    os.chdir(sys.path[0])
    print("pyctrl.py for nano.cpp on the run")
 
    # 数数cpu
    cpuCnt = cpu_count()
    maxProcess = cpu_count() - 2
    print("CPU Cores = ", cpuCnt)
 
    #####################################
    #####################################
    # 生成检测序列, 可以等间距, 也可以手动
    particleR = [3, 5, 8, 10]
    # T = numpy.arange(1.0, 3.0, 0.1)
    T = [0.5, 1, 2, 3, 4, 5]
    J = [1]
    kc = [1]
    ks = [0.5, 1.0, 2.0, 4.0]
    #####################################
    #####################################
 
    # 生成任务用命令行内容
    checkFolderFile()
    k = 0
    taskArgs = []
    print("  Num Task Commands\n-----=--------------")
    for i1 in range(len(particleR)):
        for i2 in range(len(T)):
            for i3 in range(len(J)):
                for i4 in range(len(kc)):
                    for i5 in range(len(ks)):
                        taskArgs.append(genCmd(particleR[i1], T[i2], J[i3], kc[i4], ks[i5]))
                        print("%5d " % k, taskArgs[len(taskArgs)-1])
                        k += 1
    taskTol = len(taskArgs)  # 数数总共的任务
    print("Total Task: ", taskTol)
    taskIn = 0
    ret = list(range(taskTol))
 
    # 执行计算命令
    time.sleep(3)
    while (taskIn < taskTol):
        nownanoProcess = checknanoProcess()
        if (nownanoProcess < maxProcess):
            print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
            print("Handin: " + taskArgs[taskIn])
            ret[taskIn] = os.popen(taskArgs[taskIn])
            print(
                str(taskIn) + "/" + str(taskTol) + " handed in.\t"
                "Now " + str(nownanoProcess) + " running.")
            print(ret[taskIn].read())
            taskIn += 1
        time.sleep(1)
  • home/students/nqhq/pythonmasterexe.txt
  • 最后更改: 2021/05/26 11:23
  • 由 鸟雀呼晴