Last active
April 26, 2022 10:58
-
-
Save alfredodeza/dcea71d5c0234c54d9b1 to your computer and use it in GitHub Desktop.
Real time subprocess stdout/stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment