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
# 只需要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)