def main(): # 创建服务端 handler = InterfaceHandler() processor = blur_detection.Processor(handler) # 监听端口 transport = TSocket.TServerSocket("0.0.0.0", config.THRIFT_PORT) # 选择传输层 tfactory = TTransport.TBufferedTransportFactory() # 选择传输协议 pfactory = TBinaryProtocol.TBinaryProtocolFactory() # 创建服务端 # server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory) # server.setNumThreads(12) server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory) server.setNumWorkers(config.THRIFT_NUM_WORKS) print("Starting thrift server in python...") server.serve() print("done!")
def __init__(self, processor, handler, config): try: super(ProcessPoolThriftServer, self).__init__(processor, handler, config) self._process_num = config.getint( self._section_name, "process_num", default=settings.DEFAULT_PROCESS_NUM) from thrift.server import TProcessPoolServer self._server = TProcessPoolServer.TProcessPoolServer( self._processor, self.transport, self.tfactory, self.pfactory) self._server.setNumWorkers(self._process_num) def clean_shutdown(signum, frame): for worker in self._server.workers: worker.terminate() try: threading.Thread(target=self.stop).start() except: pass def add_clean_shutdown(func): @functools.wraps(func) def wrapper(*args, **kwargs): clean_shutdown(*args, **kwargs) return func(*args, **kwargs) return wrapper sigint_handler = signal.getsignal(signal.SIGINT) if callable(sigint_handler): signal.signal(signal.SIGINT, add_clean_shutdown(sigint_handler)) else: signal.signal(signal.SIGINT, clean_shutdown) except Exception, e: self._logger.exception(e) raise e
def run(self): self.logger.info('Multi_p_t service starts to run.') self.run_log_receiver_thread() handler = SomeHandler(self.cfg, self.cache_dict) processor = SomeServer.Processor(handler) transport = TSocket.TServerSocket(port=self.port) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() workerNum = self.cfg.getint('default', 'server.workers') self.server = TProcessPoolServer.TProcessPoolServer( processor, transport, tfactory, pfactory) # 设置在worker进程启动时的callback,在worker进程重置logging config self.server.setPostForkCallback(self.resetWorkerLogConfig) self.server.setNumWorkers(workerNum) try: self.logger.info('Start multi service') self.server.serve() except Exception, why: self.logger.fatal('Serve failed, %s', why) self.server.stop() sys.exit(1)
def __init__(self, ip, port, handler_obj, service_module, num_workers=None): self._ip = ip self._port = port self._handler_obj = handler_obj self._service_module = service_module self._num_workers = num_workers or cpu_count() self._processor = service_module.Processor(self._handler_obj) self._server_socket = TSocket.TServerSocket(host=self._ip, port=self._port) self._transport = TTransport.TBufferedTransportFactory() self._protocol = TBinaryProtocol.TBinaryProtocolFactory() # 使用进程池方式 self._server = TProcessPoolServer.TProcessPoolServer( self._processor, self._server_socket, self._transport, self._protocol) self._server.setPostForkCallback(self.process_callback) self._server.setNumWorkers(self._num_workers)
def __create_server(processor, ip, port): transport = TSocket.TServerSocket(ip, port) tfactory = TTransport.TFramedTransportFactory() pfactory = TCompactProtocol.TCompactProtocolFactory() return TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory)
from example import MtExampleService, ttypes class ExampleSimpleServer(MtExampleService.Iface): def SetUserProfile(self, oProfile): #TODO return 0 def GetUserProfile(self, userId): #TODO oProfile = ttypes.UserProfile() return oProfile handler = ExampleSimpleServer() processor = MtExampleService.Processor(handler) transport = TSocket.TServerSocket('localhost', 9090) tfactory = TTransport.TBufferedTransportFactory() pfactory = TCompactProtocol.TCompactProtocolFactory() server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory) print "Starting python server..." server.serve() print "done!"
import logging logging.basicConfig() import sys sys.path.append("gen-py") from thrift.transport import TTransport from thrift.transport import TSocket from thrift.protocol import TBinaryProtocol from thrift.server import TProcessPoolServer from hello_name import HelloSvc class HelloHandler: def hello_func(self, fname, lname): print("[Server] Handling client request") return "Hello " + fname + " " + lname handler = HelloHandler() proc = HelloSvc.Processor(handler) trans_svr = TSocket.TServerSocket(port=9090) trans_fac = TTransport.TBufferedTransportFactory() proto_fac = TBinaryProtocol.TBinaryProtocolFactory() server = TProcessPoolServer.TProcessPoolServer(proc, trans_svr, trans_fac, proto_fac) print("[Server] Started") server.serve()
def main(): op = optparse.OptionParser(usage='%prog [options]', add_help_option=False) op.add_option('-p', '--port', action='store', type='int', dest='port', default=1234, help='The server port') op.add_option('-s', '--servertype', action='store', type='string', dest='servertype', default='TNonblockingServer', help='Type name of server') op.add_option('-w', '--num_workers', action='store', type='int', dest='workers', default=4, help='Number of worker processes/threads') op.add_option('-Q', '--max_queue_size', action='store', type='int', dest='max_queue_size', default=0, help='Max queue size, passed to TNonblockingServer') op.add_option('-h', '--header', action='store_true', help='Use the generated ContextIface') op.add_option('-?', '--help', action='help', help='Show this help message and exit') (options, args) = op.parse_args() if args: op.error('trailing arguments: ' + ' '.join(args)) handler = LoadHandler() processor = LoadTest.Processor(handler) if options.header: pfactory = THeaderProtocolFactory(True, [ CLIENT_TYPE.HEADER, CLIENT_TYPE.FRAMED_DEPRECATED, CLIENT_TYPE.UNFRAMED_DEPRECATED, CLIENT_TYPE.HTTP_SERVER ]) if options.servertype == 'TCppServer': print('C++ ThriftServer, Header transport, backwards compatible ' 'with all other types') elif options.servertype == 'TNonblockingServer': print('Header transport, backwards compatible with framed') else: print('Header transport, backwards compatible with ' + 'unframed, framed, http') else: if options.servertype == 'TCppServer': if not options.header: op.error('TCppServer cannot be used without header') elif options.servertype == 'TNonblockingServer': print('Framed transport') else: print('Unframed transport') pfactory = TBinaryProtocolAcceleratedFactory() if options.servertype == 'TCppServer': server = TCppServer.TCppServer(processor) server.setPort(options.port) print('Worker threads: ' + str(options.workers)) server.setNumIOWorkerThreads(options.workers) else: transport = TSocket.TServerSocket(options.port) tfactory = TTransport.TBufferedTransportFactory() if options.servertype == "TNonblockingServer": server = TNonblockingServer.TNonblockingServer( processor, transport, pfactory, maxQueueSize=options.max_queue_size) elif options.servertype == "TProcessPoolServer": server = TProcessPoolServer.TProcessPoolServer( processor, transport, tfactory, pfactory) print('Worker processes: ' + str(options.workers)) server.setNumWorkers(options.workers) else: ServerClass = getattr(TServer, options.servertype) server = ServerClass(processor, transport, tfactory, pfactory) print('Serving ' + options.servertype + ' requests on port %d...' % (options.port, )) server.serve()
def main(): op = optparse.OptionParser(usage='%prog [options]', add_help_option=False) op.add_option('-p', '--port', action='store', type='int', dest='port', default=1234, help='The server port') op.add_option('-s', '--servertype', action='store', type='string', dest='servertype', default='TGeventServer', help='Type name of server') op.add_option('-w', '--num_workers', action='store', type='int', dest='workers', default=4, help='Number of worker processes') op.add_option('-Q', '--max_queue_size', action='store', type='int', dest='max_queue_size', default=0, help='Max queue size, passed to TNonblockingServer') op.add_option('-h', '--header', action='store_true', help='Use the generated ContextIface') op.add_option('-?', '--help', action='help', help='Show this help message and exit') (options, args) = op.parse_args() if args: op.error('trailing arguments: ' + ' '.join(args)) handler = LoadHandler() if options.servertype == 'TGeventServer': # only import TGeventServer when necessary. TGeventServer calls # monkey_patch, which breaks other servers from apache.thrift.test.sync_load_handler import GeventLoadHandler from thrift.server import TGeventServer handler = GeventLoadHandler() processor = LoadTest.Processor(handler) if options.header: pfactory = THeaderProtocolFactory(True, \ [THeaderTransport.HEADERS_CLIENT_TYPE, THeaderTransport.FRAMED_DEPRECATED, THeaderTransport.UNFRAMED_DEPRECATED, THeaderTransport.HTTP_CLIENT_TYPE]) if options.servertype == 'TNonblockingServer': print 'Header transport, backwards compatible with framed' else: print 'Header transport, backwards compatible with ' + \ 'unframed, framed, http' else: if options.servertype == 'TNonblockingServer': print 'Framed transport' else: print 'Unframed transport' pfactory = TBinaryProtocolAcceleratedFactory() transport = TSocket.TServerSocket(options.port) tfactory = TTransport.TBufferedTransportFactory() if options.servertype == "TNonblockingServer": server = TNonblockingServer.TNonblockingServer( processor, transport, pfactory, maxQueueSize=options.max_queue_size) elif options.servertype == "TProcessPoolServer": server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory) print 'Worker processes: ' + str(options.workers) server.setNumWorkers(options.workers) elif options.servertype == "TGeventServer": print 'Worker processes: ' + str(options.workers) # Gevent makes its own server transport. server = TGeventServer.TGeventServer(options.port, processor, None, tfactory, pfactory) server.setNumWorkers(options.workers) else: ServerClass = getattr(TServer, options.servertype) server = ServerClass(processor, transport, tfactory, pfactory) print 'Serving ' + options.servertype + \ ' requests on port %d...' % (options.port,) server.serve()
def main(options): # set up the protocol factory form the --protocol option prot_factories = { 'binary': TBinaryProtocol.TBinaryProtocolFactory, 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory, 'compact': TCompactProtocol.TCompactProtocolFactory, 'json': TJSONProtocol.TJSONProtocolFactory, } pfactory_cls = prot_factories.get(options.proto, None) if pfactory_cls is None: raise AssertionError('Unknown --protocol option: %s' % options.proto) pfactory = pfactory_cls() try: pfactory.string_length_limit = options.string_limit pfactory.container_length_limit = options.container_limit except: # Ignore errors for those protocols that does not support length limit pass # get the server type (TSimpleServer, TNonblockingServer, etc...) if len(args) > 1: raise AssertionError( 'Only one server type may be specified, not multiple types.') server_type = args[0] # Set up the handler and processor objects handler = TestHandler() processor = ThriftTest.Processor(handler) # Handle THttpServer as a special case if server_type == 'THttpServer': server = THttpServer.THttpServer(processor, ('', options.port), pfactory) server.serve() sys.exit(0) # set up server transport and transport factory abs_key_path = os.path.join(os.path.dirname(SCRIPT_DIR), 'keys', 'server.pem') host = None if options.ssl: from thrift.transport import TSSLSocket transport = TSSLSocket.TSSLServerSocket(host, options.port, certfile=abs_key_path) else: transport = TSocket.TServerSocket(host, options.port) tfactory = TTransport.TBufferedTransportFactory() if options.trans == 'buffered': tfactory = TTransport.TBufferedTransportFactory() elif options.trans == 'framed': tfactory = TTransport.TFramedTransportFactory() elif options.trans == '': raise AssertionError('Unknown --transport option: %s' % options.trans) else: tfactory = TTransport.TBufferedTransportFactory() # if --zlib, then wrap server transport, and use a different transport factory if options.zlib: transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib tfactory = TZlibTransport.TZlibTransportFactory() # do server-specific setup here: if server_type == "TNonblockingServer": server = TNonblockingServer.TNonblockingServer( processor, transport, inputProtocolFactory=pfactory) elif server_type == "TProcessPoolServer": import signal from thrift.server import TProcessPoolServer server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory) server.setNumWorkers(5) def set_alarm(): def clean_shutdown(signum, frame): for worker in server.workers: if options.verbose > 0: logging.info('Terminating worker: %s' % worker) worker.terminate() if options.verbose > 0: logging.info('Requesting server to stop()') try: server.stop() except: pass signal.signal(signal.SIGALRM, clean_shutdown) signal.alarm(4) set_alarm() else: # look up server class dynamically to instantiate server ServerClass = getattr(TServer, server_type) server = ServerClass(processor, transport, tfactory, pfactory) # enter server main loop server.serve()
def main(options): # common header allowed client types allowed_client_types = [ THeaderTransport.THeaderClientType.HEADERS, THeaderTransport.THeaderClientType.FRAMED_BINARY, THeaderTransport.THeaderClientType.UNFRAMED_BINARY, THeaderTransport.THeaderClientType.FRAMED_COMPACT, THeaderTransport.THeaderClientType.UNFRAMED_COMPACT, ] # set up the protocol factory form the --protocol option prot_factories = { 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory(), 'multia': TBinaryProtocol.TBinaryProtocolAcceleratedFactory(), 'accelc': TCompactProtocol.TCompactProtocolAcceleratedFactory(), 'multiac': TCompactProtocol.TCompactProtocolAcceleratedFactory(), 'binary': TPedanticSequenceIdProtocolFactory( TBinaryProtocol.TBinaryProtocolFactory()), 'multi': TPedanticSequenceIdProtocolFactory( TBinaryProtocol.TBinaryProtocolFactory()), 'compact': TCompactProtocol.TCompactProtocolFactory(), 'multic': TCompactProtocol.TCompactProtocolFactory(), 'header': THeaderProtocol.THeaderProtocolFactory(allowed_client_types), 'multih': THeaderProtocol.THeaderProtocolFactory(allowed_client_types), 'json': TJSONProtocol.TJSONProtocolFactory(), 'multij': TJSONProtocol.TJSONProtocolFactory(), } pfactory = prot_factories.get(options.proto, None) if pfactory is None: raise AssertionError('Unknown --protocol option: %s' % options.proto) try: pfactory.string_length_limit = options.string_limit pfactory.container_length_limit = options.container_limit except Exception: # Ignore errors for those protocols that does not support length limit pass # get the server type (TSimpleServer, TNonblockingServer, etc...) if len(args) > 1: raise AssertionError( 'Only one server type may be specified, not multiple types.') server_type = args[0] if options.trans == 'http': server_type = 'THttpServer' # Set up the handler and processor objects handler = TestHandler() processor = ThriftTest.Processor(handler) if options.proto.startswith('multi'): secondHandler = SecondHandler() secondProcessor = SecondService.Processor(secondHandler) multiplexedProcessor = TMultiplexedProcessor() multiplexedProcessor.registerDefault(processor) multiplexedProcessor.registerProcessor('ThriftTest', processor) multiplexedProcessor.registerProcessor('SecondService', secondProcessor) processor = multiplexedProcessor global server # Handle THttpServer as a special case if server_type == 'THttpServer': if options.ssl: __certfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "server.crt") __keyfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "server.key") server = THttpServer.THttpServer(processor, ('', options.port), pfactory, cert_file=__certfile, key_file=__keyfile) else: server = THttpServer.THttpServer(processor, ('', options.port), pfactory) server.serve() sys.exit(0) # set up server transport and transport factory abs_key_path = os.path.join(os.path.dirname(SCRIPT_DIR), 'keys', 'server.pem') host = None if options.ssl: from thrift.transport import TSSLSocket transport = TSSLSocket.TSSLServerSocket(host, options.port, certfile=abs_key_path) else: transport = TSocket.TServerSocket(host, options.port, options.domain_socket) tfactory = TTransport.TBufferedTransportFactory() if options.trans == 'buffered': tfactory = TTransport.TBufferedTransportFactory() elif options.trans == 'framed': tfactory = TTransport.TFramedTransportFactory() elif options.trans == '': raise AssertionError('Unknown --transport option: %s' % options.trans) else: tfactory = TTransport.TBufferedTransportFactory() # if --zlib, then wrap server transport, and use a different transport factory if options.zlib: transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib tfactory = TZlibTransport.TZlibTransportFactory() # do server-specific setup here: if server_type == "TNonblockingServer": server = TNonblockingServer.TNonblockingServer( processor, transport, inputProtocolFactory=pfactory) elif server_type == "TProcessPoolServer": import signal from thrift.server import TProcessPoolServer server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory) server.setNumWorkers(5) def set_alarm(): def clean_shutdown(signum, frame): for worker in server.workers: if options.verbose > 0: logging.info('Terminating worker: %s' % worker) worker.terminate() if options.verbose > 0: logging.info('Requesting server to stop()') try: server.stop() except Exception: pass signal.signal(signal.SIGALRM, clean_shutdown) signal.alarm(4) set_alarm() else: # look up server class dynamically to instantiate server ServerClass = getattr(TServer, server_type) server = ServerClass(processor, transport, tfactory, pfactory) # enter server main loop server.serve()
print("Could not load THttpServer") raise else: if options.ssl: cert_path = 'thrift/test/py/test_cert.pem' transport = TSSLSocket.TSSLServerSocket(options.port, certfile=cert_path) else: transport = TSocket.TServerSocket(options.port) tfactory = TTransport.TBufferedTransportFactory() server_class_name = args[0] if args[0] == "TNonblockingServer": server = TNonblockingServer.TNonblockingServer( handler, transport, pfactory, readTimeout=options.timeout) elif args[0] == "TProcessPoolServer": server = TProcessPoolServer.TProcessPoolServer( handler, transport, tfactory, pfactory) else: ServerClass = getattr(TServer, server_class_name) server = ServerClass(handler, transport, tfactory, pfactory) if options.header: server.processor.setEventHandler(HeaderEventHandler()) elif options.context: server.processor.setEventHandler(ContextEventHandler()) server.serverEventHandler = event_handler server.serve()