Proper way to close all files after subprocess Popen and communicate

纵饮孤独 提交于 2019-12-01 08:29:57

According to this source for the subprocess module (link) if you call communicate you should not need to close the stdout and stderr pipes.

Otherwise I would try:

process.stdout.close()
process.stderr.close()

after you are done using the process object.

For instance, when you call .read() directly:

output = process.stdout.read()
process.stdout.close()

Look in the above module source for how communicate() is defined and you'll see that it closes each pipe after it reads from it, so that is what you should also do.

If you're using Twisted, don't use subprocess. If you were using spawnProcess instead, you wouldn't need to deal with annoying resource-management problems like this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!