Skip to content

Instantly share code, notes, and snippets.

@rossdylan
Created July 29, 2010 01:59
Show Gist options
  • Save rossdylan/496996 to your computer and use it in GitHub Desktop.
Save rossdylan/496996 to your computer and use it in GitHub Desktop.
"""
Used to run processes and keep the main module free to do other things.
Unfortunately this thread is designed to work under unix based systems so...
Sorry windows users :/
"""
import MODIFIED_subprocess as subProc
import os
import threading
class procThread(threading.Thread):
context = None
proc = None
procCommand = None
def __init__(self, procCommand, ctx):
threading.Thread.__init__(self)
self.procCommand = procCommand
self.context = ctx
self.logger = logging.getLogger("Process - %s" % self.procCommand)
self.logger.info("Process thread: %s" % self.procCommand)
def run(self):
if self.procCommand is not None:
self.proc = subProc.Popen(self.procCommand, stdin=subProc.PIPE, stdout=subProc.PIPE, stderr=subProc.STDOUT)
for line in self.proc.stdout:
self.context.reply(line,self.procCommand[0])
self.logger.info("Process has completed")
else:
self.context.reply("Error running process", "procThread")
def endProc(ctx):
os.kill(self.proc.pid, signal.CTRL_C_EVENT)
ctx.reply("Killed process", self.procCommand[0])
self.logger.info("Killed process: %s" % self.procCommand[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment