Last active
August 29, 2015 14:07
-
-
Save azmeuk/838fe2bf5380c5de4efa to your computer and use it in GitHub Desktop.
General output parser
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
#!/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