Here is a very basic implementation of your class running as a new process. This will allow you to continue execution within your main script/process but you will need to use some sort of interprocess communication (ie. a shared memory data structure described in multiprocessing
docs) if you want to do anything with the output of the child process within the parent.
from sys import *
from subprocess import *
import multiprocessing
class ProcessNAS(multiprocessing.Process):
def __init__ (self, name):
self.name_ = name
self.status = False
self.process = None
super(ProcessNAS, self).__init__(target=self.StartCheck)
def StartCheck(self):
print "Checking for NAS..."
stdout.flush()
self.process = Popen( ["dns-sd", "-B", "_ssh._tcp", "."], stdout=PIPE )
while True:
line = self.process.stdout.readline()[:-1]
print line
if "Add" in line and self.name_ in line:
self.status = True
print "NAS '" + self.name_ + "' is available."
elif "Rmv" in line and self.name_ in line:
self.status = False
print "NAS '" + self.name_ + "' is unavailable."
newCheckNAS = ProcessNAS("Drobo-FS")
newCheckNAS.start() # <-- instead of StartCheck(), call start()
import time
time.sleep(4)
newCheckNAS.terminate() # <-- now have two processes - able to kill the child