Proper way to close all files after subprocess Popen and communicate

前端 未结 2 1227
被撕碎了的回忆
被撕碎了的回忆 2021-01-06 02:53

We are having some problems with the dreaded \"too many open files\" on our Ubuntu Linux machine rrunning a python Twisted application. In many places in our program, we are

相关标签:
2条回答
  • 2021-01-06 02:59

    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.

    0 讨论(0)
  • 2021-01-06 03:01

    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.

    0 讨论(0)
提交回复
热议问题