我编写了一个python脚本,它产生了大约10个线程,并没有等待它们。虽然生成了一个线程,但它会创建一个bash脚本,并使用subprocess.Popen()运行它。然后,线程停止并退出。所以基本上,我有10个线程在几秒钟内终止,然后是10个正在运行的子进程。每个进程都有自己的代码,并且与其他进程无关。每个进程都终止后,脚本的运行就完成了。 该脚本重复每次运行至少15次。 问题是在第三次运行时,脚本停止并返回: [Errno 12]无法分配内存
奇怪的是,前两次运行完美地完成了他们的工作。似乎在终止子进程或线程之后,它不会释放内存。
这是一段代码:
while(True):
print "\nEXECUTION "+str(j)+":"
# CUTTED CODE
threads = []
print "Starting parallel execution of subfiles processing..."
for t in range(numfiles): # generate t threads
thread = threading.Thread(target=processing,args=(inputfile,subsize,t+1,j,))
thread.start()
# start waiting loop for all the waitfiles
#WHILE LOOPS that waits here
print "Executed all "+str(numfiles)+" processes!"
j+=1
if(j>=maxrun):
print "Maximum number of runs reached. The computation will stops."
print "Renaming final output file...",
print "done."
break
#END MAIN WHILE
def processing(inputfile,subsize,i,j):
#create file .sh
shpath = shell+"script_"+str(i)+"_"+str(j)+".sh"
fp = open(shpath,"w",os.O_NONBLOCK)
cmd = os.path.abspath(shell)+"/script_"+str(i)+"_"+str(j)+".sh"
cmd = shell+"./script_"+str(i)+"_"+str(j)+".sh"
while True:
try:
subprocess.Popen([cmd],stderr=subprocess.STDOUT, stdout=subprocess.PIPE,close_fds=True)
except OSError as e:
print (e.strerror+" Cannot execute the process script_"+str(i)+"_"+str(j)+".sh!\nRetrying...")
continue
break
print "Runned script_"+str(i)+"_"+str(j)+".sh"