class LocalProcess(object): STOP_SIGNALS = ((signal.SIGINT, 1), (signal.SIGTERM, 5), (signal.SIGKILL, 0)) def __init__(self, command, arguments, environment=None, stdout=None): self.__command = command log.debug('%s %s', command, ' '.join(arguments)) self.__process = Popen([command] + arguments, executable=command, cwd=os.getcwd(), env=environment, stdout=stdout, stderr=subprocess.STDOUT, preexec_fn=os.setpgrp) def terminate(self): for sig, timeout in self.STOP_SIGNALS: try: log.debug('Killing process group %s with signal %s', self.__process.pid, sig) os.killpg(self.__process.pid, sig) except OSError: pass giveup_time = time.time() + timeout while self.__process.poll() is None: if time.time() > giveup_time: break time.sleep(0.1) if self.__process.poll() is not None: break self.__process.wait() self.__process = None def command(self): return self.__command def pid(self): if self.__process: return self.__process.pid else: return None def isAlive(self): return self.__process and self.__process.poll() is None
class LocalProcess(object): STOP_SIGNALS = ((signal.SIGINT, 1), (signal.SIGTERM, 5), (signal.SIGKILL, 0)) def __init__(self, command, arguments, environment=None, stdout=None): self.__command = command log.debug('%s %s', command, ' '.join(arguments)) self.__process = Popen([command]+arguments, executable=command, cwd=os.getcwd(), env=environment, stdout=stdout, stderr=subprocess.STDOUT, preexec_fn=os.setpgrp) def terminate(self): for sig, timeout in self.STOP_SIGNALS: try: log.debug('Killing process group %s with signal %s', self.__process.pid, sig) os.killpg(self.__process.pid, sig) except OSError: pass giveup_time = time.time() + timeout while self.__process.poll() is None: if time.time() > giveup_time: break time.sleep(0.1) if self.__process.poll() is not None: break self.__process.wait() self.__process = None def command(self): return self.__command def pid(self): if self.__process: return self.__process.pid else: return None def isAlive(self): return self.__process and self.__process.poll() is None
class LocalProcess(object): STOP_SIGNALS = ((signal.SIGINT, 1), (signal.SIGTERM, 5), (signal.SIGKILL, 0)) def __init__(self, command, arguments, environment=None, stdout=None): self.__terminateRequested = False self.__command = command self.__arguments = arguments log.debug('%s %s', command, ' '.join(arguments)) self.__process = Popen([command] + arguments, executable=command, cwd=os.getcwd(), env=environment, stdout=stdout, stderr=subprocess.STDOUT, preexec_fn=os.setpgrp) self.__tracker = threading.Thread(target=self.monitorChild) self.__tracker.daemon = True self.__tracker.start() def monitorChild(self): pid = self.__process.pid try: self.__process.communicate()[0] if self.__terminateRequested or self.__process.returncode == 0: return for idx in range(len(self.__arguments)): if self.__arguments[idx] == 'NAME_BINDING': if len(self.__arguments) >= idx + 1: print 'Component ' + self.__arguments[ idx + 1] + ' (pid=' + str(pid) + ') has died' else: print 'Component with process id ' + str( pid) + 'has died' except: if self.__terminateRequested or self.__process.returncode == 0: return print 'Component with process id ' + str(pid) + 'has died' def terminate(self): for sig, timeout in self.STOP_SIGNALS: try: log.debug('Killing process group %s with signal %s', self.__process.pid, sig) os.killpg(self.__process.pid, sig) except OSError: pass giveup_time = time.time() + timeout while self.__process.poll() is None: if time.time() > giveup_time: break time.sleep(0.1) if self.__process.poll() is not None: break self.__process.wait() self.__process = None def requestTermination(self): self.__terminateRequested = True def command(self): return self.__command def pid(self): if self.__process: return self.__process.pid else: return None def isAlive(self): return self.__process and self.__process.poll() is None
class LocalProcess(object): STOP_SIGNALS = ((signal.SIGINT, 1), (signal.SIGTERM, 5), (signal.SIGKILL, 0)) def __init__(self, command, arguments, environment=None, stdout=None): self.__terminateRequested = False self.__command = command self.__arguments = arguments log.debug('%s %s', command, ' '.join(arguments)) self.__process = Popen([command] + arguments, executable=command, cwd=os.getcwd(), env=environment, stdout=stdout, stderr=subprocess.STDOUT, preexec_fn=os.setpgrp) self.__tracker = None self.__callback = None self.__children = [] def setTerminationCallback(self, callback): if not self.__tracker: # Nothing is currently waiting for notification, start monitor. name = 'process-%d-tracker' % self.pid() self.__tracker = threading.Thread(name=name, target=self._monitorProcess) self.__tracker.daemon = True self.__tracker.start() self.__callback = callback def _monitorProcess(self): try: status = self.__process.wait() except: # If wait fails, don't bother with notification. return if self.__callback: self.__callback(self.pid(), status) def terminate(self): for child in self.__children: child.terminate() self.__children = [] for sig, timeout in self.STOP_SIGNALS: try: log.debug('Killing process group %s with signal %s', self.__process.pid, sig) os.killpg(self.__process.pid, sig) except OSError: pass giveup_time = time.time() + timeout while self.__process.poll() is None: if time.time() > giveup_time: break time.sleep(0.1) if self.__process.poll() is not None: break self.__process.wait() self.__process = None def requestTermination(self): self.__terminateRequested = True def command(self): return self.__command def pid(self): if self.__process: return self.__process.pid else: return None def isAlive(self): return self.__process and self.__process.poll() is None def addChild(self, process): self.__children.append(process)
class LocalProcess(object): STOP_SIGNALS = ((signal.SIGINT, 1), (signal.SIGTERM, 5), (signal.SIGKILL, 0)) def __init__(self, command, arguments, environment=None, stdout=None): self.__terminateRequested = False self.__command = command self.__arguments = arguments log.debug("%s %s", command, " ".join(arguments)) self.__process = Popen( [command] + arguments, executable=command, cwd=os.getcwd(), env=environment, stdout=stdout, stderr=subprocess.STDOUT, preexec_fn=os.setpgrp, ) self.__tracker = threading.Thread(target=self.monitorChild) self.__tracker.daemon = True self.__tracker.start() def monitorChild(self): pid = self.__process.pid try: self.__process.communicate()[0] if self.__terminateRequested or self.__process.returncode == 0: return for idx in range(len(self.__arguments)): if self.__arguments[idx] == "NAME_BINDING": if len(self.__arguments) >= idx + 1: print "Component " + self.__arguments[idx + 1] + " (pid=" + str(pid) + ") has died" else: print "Component with process id " + str(pid) + "has died" except: if self.__terminateRequested or self.__process.returncode == 0: return print "Component with process id " + str(pid) + "has died" def terminate(self): for sig, timeout in self.STOP_SIGNALS: try: log.debug("Killing process group %s with signal %s", self.__process.pid, sig) os.killpg(self.__process.pid, sig) except OSError: pass giveup_time = time.time() + timeout while self.__process.poll() is None: if time.time() > giveup_time: break time.sleep(0.1) if self.__process.poll() is not None: break self.__process.wait() self.__process = None def requestTermination(self): self.__terminateRequested = True def command(self): return self.__command def pid(self): if self.__process: return self.__process.pid else: return None def isAlive(self): return self.__process and self.__process.poll() is None