我正在使用子进程来调用另一个程序,并将其返回值保存到变量中。循环重复此过程,经过数千次后程序崩溃,并出现以下错误:
Traceback (most recent call last): File "./extract_pcgls.py", line 96, in <module> SelfE.append( CalSelfEnergy(i) ) File "./extract_pcgls.py", line 59, in CalSelfEnergy p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) File "/usr/lib/python3.2/subprocess.py", line 745, in __init__ restore_signals, start_new_session) File "/usr/lib/python3.2/subprocess.py", line 1166, in _execute_child errpipe_read, errpipe_write = _create_pipe() OSError: [Errno 24] Too many open files
任何解决该问题的想法都非常感谢!
注释提供的代码:
cmd = "enerCHARMM.pl -parram=x,xtop=topology_modified.rtf,xpar=lipid27_modified.par,nobuildall -out vdwaals {0}".format(cmtup[1]) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) out, err = p.communicate()
我猜问题是由于我正在处理带有子进程的打开文件:
cmd = "enerCHARMM.pl -par param=x,xtop=topology_modified.rtf,xpar=lipid27_modified.par,nobuildall -out vdwaals {0}".format(cmtup[1]) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
在此,cmd变量包含刚刚创建但尚未关闭的文件的名称。然后,subprocess.Popen在该文件上调用系统命令。多次执行此操作后,程序崩溃并显示该错误消息。
subprocess.Popen
所以我从中学到的信息是
关闭您创建的文件,然后进行处理