python 实时子进程stdout / stderr

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 实时子进程stdout / stderr相关的知识,希望对你有一定的参考价值。

import logging
import threading
import os
import subprocess

logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)


class LogPipe(threading.Thread):

    def __init__(self, level):
        """Setup the object with a logger and a loglevel
        and start the thread
        """
        threading.Thread.__init__(self)
        self.daemon = False
        self.level = level
        self.fdRead, self.fdWrite = os.pipe()
        self.pipeReader = os.fdopen(self.fdRead)
        self.process = None
        self.start()

    def fileno(self):
        """Return the write file descriptor of the pipe
        """
        return self.fdWrite

    def run(self):
        """Run the thread, logging everything.
        """
        for line in iter(self.pipeReader.readline, ''):
            logging.log(self.level, line.strip('\n'))

        self.pipeReader.close()

    def close(self):
        """Close the write end of the pipe.
        """
        os.close(self.fdWrite)

    def stop(self):
        self._stop = True
        self.close()

    def __del__(self):
        try:
            self.stop()
        except:
            pass
        try:
            del self.fdRead
            del self.fdWrite
        except:
            pass

# For testing
if __name__ == "__main__":
    logpipe = LogPipe(logging.INFO)
    p = subprocess.Popen(
        ['python', 'mixed.py'],
        stdout=logpipe,
        stderr=logpipe
    )

    returncode = p.wait()
    logpipe.close()
    import sys
    sys.exit
    
# cat mixed.py
    
# import sys
# 
# def stderr(string):
#     sys.stderr.write(string)
#     sys.stderr.flush()
# 
# def stdout(string):
#     sys.stdout.write(string)
#     sys.stdout.flush()
# 
# stdout('this is an stdout line\n')
# stdout('this is an stdout line\n')
# stderr('this is an stderr line\n')
# stderr('this is an stderr line\n')
# stderr('this is an stderr line\n')
# stdout('this is an stdout line\n')
# stderr('this is an stderr line\n')
# stdout('this is an stdout line\n')

以上是关于python 实时子进程stdout / stderr的主要内容,如果未能解决你的问题,请参考以下文章

子进程在终止前获取结果

Python:在记录内存时获取子进程 STDOUT

管道时在python的子进程模块中使用stdout.close()

Python监控子进程的stderr和stdout

Python asyncio子进程连续写入stdin和读取stdout/stderr

在没有flush()和新行的子进程输出上进行非阻塞读取