我有一个带有 perl 工作子进程的长时间运行的 python 脚本。数据通过其 stdin 和 stdout 传入和传出子进程。必须定期重新启动子进程。
不幸的是,运行一段时间后,文件用完了(“打开的文件太多”)。lsof 显示了许多剩余的开放管道。
在 Popen'd 进程之后清理的正确方法是什么?这就是我现在正在做的事情:
def start_helper(self):
# spawn perl helper
cwd = os.path.dirname(__file__)
if not cwd:
cwd = '.'
self.subp = subprocess.Popen(['perl', 'theperlthing.pl'], shell=False, cwd=cwd,
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
bufsize=1, env=perl_env)
def restart_helper(self):
# clean up
if self.subp.stdin:
self.subp.stdin.close()
if self.subp.stdout:
self.subp.stdout.close()
if self.subp.stderr:
self.subp.stderr.close()
# kill
try:
self.subp.kill()
except OSError:
# can't kill a dead proc
pass
self.subp.wait() # ?
self.start_helper()