# -*- coding: utf-8 -*-
"""
Created on Mon May 27 14:20:58 2013

@author: MarkusR
"""

import threading
import subprocess
import time

class ExternalScriptClass(threading.Thread):
    """
    Class to start an external script in a non blocking way

    Parameters
    ----------
    command: (string) 
        full command to start external
        e.g. 'python.exe input.py'

    Note
    ----------       
    copied from        
    http://stackoverflow.com/questions/984941/python-subprocess-popen-from-a-thread
    """    
    
    def __init__(self, command):

        self.stdout = None
        self.stderr = None
        threading.Thread.__init__(self)
        self.cmd = command

    def run(self):
        
        self.p = subprocess.Popen(self.cmd.split(),
                             shell=False,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
                             
        print "started", self.cmd
        self.stdout, self.stderr = self.p.communicate()
    
if __name__ == '__main__':  
    MyScript = ExternalScriptClass('python.exe test.py')
    MyScript.start()
    
    while 1:
        #run forever
        time.sleep(1)