Well, thanks Dvir for letting me try to look into the thrift python TServer implementation code (server/TServer.py).
*class TSimpleServer(TServer):* * def serve(self):* * self.serverTransport.listen()* * while True:* * client = self.serverTransport.accept()* * itrans = self.inputTransportFactory.getTransport(client)* * otrans = self.outputTransportFactory.getTransport(client)* * iprot = self.inputProtocolFactory.getProtocol(itrans)* * oprot = self.outputProtocolFactory.getProtocol(otrans)* * try:* * while True:* * self.processor.process(iprot, oprot)* * except TTransport.TTransportException, tx:* * pass* * except Exception, x:* * logging.exception(x)* * * * itrans.close()* * otrans.close()* So using TSimpleServer, it's not possible to raise any exception that could fight against that "except Exception, x" line. Actually it's nearly same situation on other TServer implementations. I think maybe one can define TThriftShutdownRequest exception and let TServer implementations would return when it is raised. Would this be possible? If it make sense, could I open a JIRA issue about this, providing a patch with it? (I'm very new to open-source group) 2011/8/31 Dvir Volk <[email protected]> > well, you could always do (assuming you have 2 threads) > > on the serving thread, you start with this function: > def startServer() > try: > #init code yada yada > > #start it up! > server.serve() > except (SystemExit, KeyboardInterrupt): > #any shutdown code, closing sockets goes here > return > > > and on the other thread: > raise SystemExit > > or even serverthread.terminate() > > > > > > > On Tue, Aug 30, 2011 at 9:51 PM, Francisco Byun <[email protected]> > wrote: > > Same question here. > > > > I'm trying to write a test code for our project, which temporarily > creates a > > TServer object and serve() it on a thread, and perform test codes, and > then > > terminate it. But I just can't figure out howto. I think if TServer > > implementation had stop() method, I can try: (psuedo code) > > > > *import thread* > > *server_instance, handler_instance = get_thrift_server()* > > * > > * > > *thread.start_new_thread(server_instance.serve, tuple())* > > * > > * > > *run_tests()* > > * > > * > > *server_instance.stop()* > > > > I tried "sys.exit(0)" and many other ways, but all failed to terminate > the > > thread which runs TServer.serve() ... Any suggestions? > > > > -- > > Francisco Byun > > > > 2011/8/11 Kyle <[email protected]> > > > >> There doesn't seem to be a 'stop' method in any of the python TServer > >> implementations. I know the method exists in the Java API. > >> I'd like to have a second thread watching activity, and when the > >> server seems inactive, hit the stop button and shutdown the program. > >> But all of the server loops are 'while True' and waiting for an > >> exception. > >> > >> Any ideas? > >> > >> Kyle > >> > > > > > > -- > Dvir Volk > System Architect, DoAT, http://doat.com >
