def run(self): log.info('Starting REST server') # Handle SIGTERM the same way as SIGINT. def stop_server(signum, frame): log.info('REST server shutdown') sys.exit(signal.SIGTERM) signal.signal(signal.SIGTERM, stop_server) try: make_server().serve_forever() except KeyboardInterrupt: log.info('REST server interrupted') sys.exit(signal.SIGINT) except select.error as (errcode, message): if errcode == errno.EINTR: log.info('REST server exiting') sys.exit(errno.EINTR) raise
def __init__(self, name, slice=None): """See `IRunner`.""" super().__init__(name, slice) # Both the REST server and the signal handlers must run in the main # thread; the former because of SQLite requirements (objects created # in one thread cannot be shared with the other threads), and the # latter because of Python's signal handling semantics. # # Unfortunately, we cannot issue a TCPServer shutdown in the main # thread, because that will cause a deadlock. Yay. So what we do is # to use the signal handler to notify a shutdown thread that the # shutdown should happen. That thread will wake up and stop the main # server. self._server = make_server() self._event = threading.Event() def stopper(event, server): # noqa event.wait() server.shutdown() self._thread = threading.Thread( target=stopper, args=(self._event, self._server)) self._thread.start()
def __init__(self, name, slice=None): """See `IRunner`.""" super().__init__(name, slice) # Both the REST server and the signal handlers must run in the main # thread; the former because of SQLite requirements (objects created # in one thread cannot be shared with the other threads), and the # latter because of Python's signal handling semantics. # # Unfortunately, we cannot issue a TCPServer shutdown in the main # thread, because that will cause a deadlock. Yay. So what we do is # to use the signal handler to notify a shutdown thread that the # shutdown should happen. That thread will wake up and stop the main # server. self._server = make_server() self._event = threading.Event() def stopper(event, server): # noqa: E306 event.wait() server.shutdown() self._thread = threading.Thread( target=stopper, args=(self._event, self._server)) self._thread.start()