def shutdown(self, timeout=1.0): """Shutdown the subprocess. This method tries to shutdown the subprocess cleanly, waits until timeout expires, then terminates it. """ if not self.is_running: return # we're about to shut down, tell our responder trapcall.trap_call("subprocess shutdown", self.responder.on_shutdown) # Politely ask our process to shutdown self.send_quit() # If things go right, the process will quit, then our thread will # quit. Wait for a clean shutdown self.thread.join(timeout) # If things didn't shutdown, then force them to quit. Let's not # bother with SIGTERM since that really also would be an abnormal # exit as far as the child is concerned. try: self.process.kill() except OSError, e: # Error on kill. Just log an error and move on. Nothing # much we can do here anyway. logging.exception('worker subprocess kill failed')
def process_handler_queue(self): # this executes in the event loop thread. Here's where we should call # handler methods # Before we get anything from our queue, we must set # safe_to_skip_add_idle to False self.safe_to_skip_add_idle = False while not self.handler_queue.empty(): (method, message) = self.handler_queue.get() trapcall.trap_call('processing handler method', method, message)
def restart(self, clean=False): if clean: self.shutdown() else: # close our stream to the subprocess self.process.stdin.close() # unset our attributes for the process that just quit. This protects # us in case _start() fails for some reason. self._cleanup_process() # restart ourselves self._start() trapcall.trap_call("subprocess restart", self.responder.on_restart)
def _start(self): """Does the work to startup a new process/thread.""" # create our child process. self.process = self._start_subprocess() # create thread to handle the subprocess's output. It would be nice # to eliminate this thread, but I don't see an easy way to integrate # it into the eventloop, since windows doesn't have support for # select() on pipes. # # This thread only handles the subprocess output. We write to the # subprocess stdin from the eventloop. self.thread = SubprocessResponderThread(self.process.stdout, self.responder, self._on_thread_quit) self.thread.daemon = True self.thread.start() # work is all done, do some finishing touches self.is_running = True self.sent_quit = False self._send_startup_info() trapcall.trap_call("subprocess startup", self.responder.on_startup)