Skip to content

Instantly share code, notes, and snippets.

@azmeuk
Last active August 29, 2015 14:07
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 azmeuk/838fe2bf5380c5de4efa to your computer and use it in GitHub Desktop.
Save azmeuk/838fe2bf5380c5de4efa to your computer and use it in GitHub Desktop.
General output parser
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import multiprocessing, subprocess, sys
class Parser(multiprocessing.Process):
process = None
def __init__(self, daemon, output):
super(Parser, self).__init__()
self.daemon = daemon
self.output = output
def start(self):
if self.daemon:
self.launchFoobar() # Foobar is launched before forking
super(Parser, self).start()
def run(self):
if not self.daemon:
self.launchFoobar() # Foobar is launched after forking
self.parseFoobar()
def launchFoobar(self):
self.process = subprocess.Popen(["python", "foobar.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
def parseFoobar(self):
with open(self.output, "w+") as f:
for line in iter(self.process.stdout):
f.write(line)
f.flush()
self.process.wait()
if __name__ == "__main__":
async = Parser(True, "async.txt")
async.start()
sync = Parser(False, "sync.txt")
sync.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment