def kill_parent(self): for pid_file in self.get_pid_files(): if os.path.basename(pid_file).startswith('c'): continue with open(pid_file, "r") as f: pid = f.read().strip() kill_process(int(pid))
def wait_for_stop_and_then_terminate(self, timeout=30): clean_timeout = timeout - 1 start_time = time.time() if self.CHILD_COMMS_STRATEGY: logger.debug("Waiting for child processes to terminate") self._wait_for_children_to_terminate(start_time, clean_timeout) if self.child_processes: #We've nearly run out of time - let's try and kill them: logger.info("Attempting to kill child processes") for p in list(self.child_processes): try: kill_process(p._process_instance.pid) except Exception as e: logger.warning("Failed to kill child process with PID %s: %s\n%s", p._process_instance.pid, e, _traceback_str()) self._wait_for_children_to_terminate(start_time, timeout)
def wait_for_stop_and_then_terminate(self, timeout=30): """Waits for children to stop, but terminates them if necessary. Returns the number terminated""" clean_timeout = timeout - 1 start_time = time.time() if self.CHILD_COMMS_STRATEGY.CAN_WAIT_FOR_TERMINATE: logger.debug("Waiting for child processes to terminate") self._wait_for_children_to_terminate(start_time, clean_timeout) num_terminated = 0 if self.child_processes: #We've nearly run out of time - let's try and kill them: logger.info("Attempting to kill child processes") for p in list(self.child_processes): try: num_terminated += 1 if process_exists(p.pid): kill_process(p.pid) except Exception as e: logger.warning("Failed to kill child process %s with PID %s: %s\n%s", p.name, p.pid, e, _traceback_str()) self._wait_for_children_to_terminate(start_time, timeout) return num_terminated
def kill_process_ignore_access_denied(self, pid): try: return kill_process(pid) except AccessDeniedError as e: #Can't do anything about this pass
from processfamily.processes import kill_process import time def get_path_to_ParentProcessPy(): return os.path.join(os.path.dirname(__file__), 'ParentProcess.py') if __name__ == '__main__': parent_process = subprocess.Popen( [Config.pythonw_exe, get_path_to_ParentProcessPy()], close_fds=True) def wait_for_end(): try: while True: print('Waiting') if parent_process.poll() is not None: print('Ended') return time.sleep(1) except Exception as e: print(_traceback_str()) t = threading.Thread(target=parent_process.wait) t.start() try: while t.is_alive(): t.join(1) except Exception as e: kill_process(parent_process.pid) print("Done")
from processfamily.test import Config from processfamily.processes import kill_process import time def get_path_to_ParentProcessPy(): return os.path.join(os.path.dirname(__file__), 'ParentProcess.py') if __name__ == '__main__': parent_process = subprocess.Popen( [Config.pythonw_exe, get_path_to_ParentProcessPy()], close_fds=True) def wait_for_end(): try: while True: print 'Waiting' if parent_process.poll() is not None: print 'Ended' return time.sleep(1) except Exception as e: print _traceback_str() t = threading.Thread(target=parent_process.wait) t.start() try: while t.isAlive(): t.join(1) except Exception as e: kill_process(parent_process.pid) print "Done"