Proper way to close all files after subprocess Popen and communicate

前端 未结 2 1226
被撕碎了的回忆
被撕碎了的回忆 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 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.

提交回复
热议问题