Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awesomebytes/d6eef176c7def07a9c2df4b6c4ed6501 to your computer and use it in GitHub Desktop.
Save awesomebytes/d6eef176c7def07a9c2df4b6c4ed6501 to your computer and use it in GitHub Desktop.
Parse output in real time of any program in Python efficiently
#!/usr/bin/env python
# To make Python 2.7 print an actual function
from __future__ import print_function
import subprocess
# shlex takes care of splitting a string representing a command in words for subprocess
import shlex
import os
class RealTimeProgramOutputParser(object):
"""
Given a command and a function to execute, it executes that function for every new line
from the output of the command.
If the program ends a RuntimeError exception is raised.
E.g.: `t = RealTimeProgramOutputParser('dmesg --follow', print)`
Will print every dmesg line as they come.
"""
def __init__(self, command, function_for_every_new_line):
# Run the process
self._process = subprocess.Popen(shlex.split(command),
shell=False,
# We pipe the output to an internal pipe
stdout=subprocess.PIPE,
# Make sure that if this Python program is killed, this gets killed too
preexec_fn=os.setsid)
# Poll for output, note: readline() blocks efficiently until there is output with a newline
while True:
output = self._process.stdout.readline()
# Polling returns None when the program is still running, return_code otherwise
return_code = self._process.poll()
if return_code is not None:
# Program ended, get exit/return code
raise RuntimeError("Command '{}' finished with exit code {}".format(command, return_code))
# If the output is not empty, feed it to the function, strip the newline first
if output:
function_for_every_new_line(output.strip())
if __name__ == '__main__':
rtpop = RealTimeProgramOutputParser('dmesg --follow', print)
@awesomebytes
Copy link
Author

I've never used fileinput, so I don't know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment