Python 子进程:打开的文件太多 [英] Python Subprocess: Too Many Open Files
问题描述
我正在使用子进程调用另一个程序并将其返回值保存到一个变量中.这个过程在一个循环中重复,在几千次之后程序崩溃并出现以下错误:
I am using subprocess to call another program and save its return values to a variable. This process is repeated in a loop, and after a few thousands times the program crashed with the following error:
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
非常感谢您对如何解决此问题的任何想法!
Any idea how to solve this issue is much appreciated!
从评论提供的代码:
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()
推荐答案
我想问题是由于我正在使用子进程处理打开的文件:
I guess the problem was due to the fact that I was processing an open file with subprocess:
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
对该文件调用系统命令.多次执行此操作后,程序崩溃并显示该错误消息.
Here the cmd variable contain the name of a file that has just been created but not closed. Then the subprocess.Popen
calls a system command on that file. After doing this for many times, the program crashed with that error message.
所以我从中学到的信息是
So the message I learned from this is
关闭您创建的文件,然后对其进行处理
Close the file you have created, then process it
这篇关于Python 子进程:打开的文件太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!