def serve(self): if self.daemonize and os.environ.get('DAEMON', None) != 'yes': os.environ['DAEMON'] = 'yes' util.background() if not self.is_setup: self.setup() self.ready.set() try: while not self.shutting_down: self.handle_packet(*self.socket.recvfrom(self.max_packet_size)) if not self.shutting_down: greenhouse.pause() except KeyboardInterrupt: pass finally: self.cleanup()
def serve(self): """run the server at the provided address forever. this method will remove the calling greenlet (generally the main greenlet) from the scheduler, so don't expect anything else to run in the calling greenlet until the server has been shut down. """ if self.daemonize and os.environ.get('DAEMON', None) != 'yes': os.environ['DAEMON'] = 'yes' util.background() if not self.is_setup: self.setup() self.ready.set() try: while not self.shutting_down: try: client_sock, client_address = self.socket.accept() handler = self.connection_handler( client_sock, client_address, self) except socket.error, error: if error.args[0] in (errno.ENFILE, errno.EMFILE): # max open connections greenhouse.pause_for(0.01) elif error.args[0] == errno.EINVAL: # server socket was shut down break elif error.args[0] == errno.EBADF: # believe it or not, this is the graceful shutdown # case. see the comments in shutdown() below break raise else: # might be a long time before the next accept call returns greenhouse.schedule(handler.serve_all) del handler, client_sock except KeyboardInterrupt: pass finally: self._cleanup()