def __init__(self): self._iptranfac = TTransport.TFramedTransportFactory() self._optranfac = TTransport.TFramedTransportFactory() self._iprotfac = TBinaryProtocol.TBinaryProtocolFactory() self._oprotfac = TBinaryProtocol.TBinaryProtocolFactory() self._processMap = {} self._processMap["predict"] = Receiver.process_predict self._processMap["ping"] = Receiver.process_ping
def __init__(self, policy): #, expected_instance_cnt): self.instance_list = {} self.ip = wieraCommon.get_public_ip() self.port = 0 self.policy = policy self.monitoring_info = {} # set handler to our implementation # to avoid any sync issue with portnumber handler = WieraTieraInstanceHandler(policy, self) processor = WieraTieraInstanceIface.Processor(handler) self.transport = TSocket.TServerSocket(None) tfactory = TTransport.TFramedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() # set server self.server = TServer.TThreadPoolServer(processor, self.transport, tfactory, pfactory, daemon=True) self.port = self.transport.port #set socket thread 20 min self.server.setNumThreads(64) #Thrift Server Start self.instance_manager_thread = threading.Thread( target=self.run_forever, args=()) self.instance_manager_thread.daemon = True self.instance_manager_thread.start()
def run(self): processor = TMultiplexedProcessor() processor.registerProcessor( "DFSService", DistributedFilesystem.Processor( DistributedFileSystemHandler(self.master.dfs) ) ) processor.registerProcessor( "UserService", MasterUserService.Processor( UserServiceHandler(self.master) ) ) server = TServer.TThreadedServer( processor, TSocket.TServerSocket(self.listen_address, self.listen_port), TTransport.TFramedTransportFactory(), TBinaryProtocol.TBinaryProtocolFactory(), daemon=True, ) # Start Tanit distributed filesystem server.serve()
def get_server(api, handler, port, **kwargs): processor = api.Processor(handler) socket = TSocket.TServerSocket(port=port) tfactory = kwargs.get('transport') or TTransport.TFramedTransportFactory() pfactory = kwargs.get('protocol') or TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TThreadedServer(processor, socket, tfactory, pfactory) return server
def create_server(handler, port): processor = InstanceService.Processor(handler) transport = TSocket.TServerSocket(port=port) tfactory = TTransport.TFramedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) return server
def run(self, host='0.0.0.0', port='5050'): from .handler.handler import Model from .ThriftZMQ.server.TModelProcessor import Processor from .ThriftZMQ.server.TModelPoolServer import TModelPoolServer from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol transport = TSocket.TServerSocket(host=host, port=port) tfactory = TTransport.TFramedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() print('========================================') print('Multiplication Server') print('Host:', host) print('Port:', port) print('========================================') listModelConfig = [{"gpu_id": '-1', 'mem_fraction': 0.2}, {"gpu_id": '-1', 'mem_fraction': 0.2}] server = TModelPoolServer(Model, listModelConfig, Processor, transport, tfactory, pfactory, timeout=0.1, batchSize=10) server.serve()
def open_spider(self, spider): self._socket = TSocket.TSocket(self.host, self.port, self.unix_socket) self._ts_factory = TTransport.TFramedTransportFactory() self._transport = self._ts_factory.getTransport(self._socket) self._protocol = TCompactProtocol.TCompactProtocol(self._transport) self.client = ThriftSourceProtocol.Client(iprot=self._protocol, oprot=self._protocol) self.connect()
def echo_server(): mp = TMultiplexedProcessor.TMultiplexedProcessor() mp.registerProcessor("echo", Echoer.Processor(EchoHandler())) transport = TSocket.TServerSocket(port=PORT) tf = TTransport.TFramedTransportFactory() pf = TCompactProtocol.TCompactProtocolFactory() return TServer.TThreadedServer(mp, transport, tf, pf, daemon=True)
def __init__(self, thrift_host, thrift_port, timeout=None, unix_socket=None): self.thrift_host = thrift_host self.thrift_port = thrift_port self.timeout = timeout self.unix_socket = unix_socket self._socket = TSocket.TSocket(self.thrift_host, self.thrift_port, self.unix_socket) self._transport_factory = TTransport.TFramedTransportFactory() self._transport = self._transport_factory.getTransport(self._socket)
def serve(conf_path, storage_factory=None): """This method starts the server. There are two processes, one is an HTTP server that shows and admin interface and the second is a Thrift server that the client code calls. Arguments: `conf_path` - The path to your flawless.cfg file `storage_factory` - You can pass in your own storage class that implements StorageInterface. You must implement storage_cls if you want Flawless to be horizontally scalable, since by default it will just store everything on the local disk. """ flawless.lib.config.init_config(conf_path) # Try and create datadir if it doesn't exist. For instance it might be in /tmp if not os.path.exists(config.data_dir_path): os.makedirs(config.data_dir_path) storage_factory = storage_factory or ( lambda partition: DiskStorage(partition=partition)) # Setup root logger root_logger = logging.getLogger() root_handler = logging.handlers.TimedRotatingFileHandler( filename=config.log_file, when='d', interval=1, backupCount=config.log_days_to_keep) root_logger.setLevel(getattr(logging, config.log_level)) root_logger.addHandler(root_handler) child_pid = os.fork() if child_pid == 0: # Setup HTTP server handler = FlawlessWebServiceHandler(storage_factory=storage_factory) server = SimpleThreadedHTTPServer(('', config.http_port), SimpleRequestHTTPHandler) server.attach_service(handler) server.request_queue_size = 50 try: server.serve_forever() except (KeyboardInterrupt, SystemExit): server.server_close() else: # Setup Thrift server handler = FlawlessThriftServiceHandler(storage_factory=storage_factory) processor = Flawless.Processor(handler) transport = TSocket.TServerSocket(port=config.port) tfactory = TTransport.TFramedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TThreadedServer(processor, transport, tfactory, pfactory) try: server.serve() except (KeyboardInterrupt, SystemExit): handler.errors_seen.sync() transport.close() os.kill(child_pid, signal.SIGINT)
def startup(self): print('启动Thrift消息服务器') processor = MessageServiceApi.Processor(self) transport = TSocket.TServerSocket('0.0.0.0', '9900') tfactory = TTransport.TFramedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) print('startup...') server.serve() print('Bye')
def server(): handler = EventHandlerHandler() processor = EventHandler.Processor(handler) transport = TSocket.TServerSocket(port=PORT) tfactory = TTransport.TFramedTransportFactory() pfactory = TCompactProtocol.TCompactProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) print "Starting python server..." server.serve()
def wrapWorker(impl): processor = QbeastWorker.Processor(impl) transport = TSocket.TServerSocket(port=2688) tfactory = TTransport.TFramedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = IgnoreServer( TServer.TThreadedServer(processor, transport, tfactory, pfactory)) t = Process(target=server.serve) t.daemon = True t.start() return server, t
def __init__(self, log, listener, processor, inputTransportFactory=None, outputTransportFactory=None, inputProtocolFactory=None, outputProtocolFactory=None, **kwargs): StreamServer.__init__(self, listener, self._process_socket, **kwargs) self.log = log self.processor = processor self.inputTransportFactory = (inputTransportFactory or TTransport.TFramedTransportFactory()) self.outputTransportFactory = (outputTransportFactory or TTransport.TFramedTransportFactory()) self.inputProtocolFactory = (inputProtocolFactory or TBinaryProtocol.TBinaryProtocolFactory()) self.outputProtocolFactory = (outputProtocolFactory or TBinaryProtocol.TBinaryProtocolFactory())
def run(self): # Create Service handler handler = WorkerServiceHandler(self.master) server = TServer.TThreadedServer( MasterWorkerService.Processor(handler), TSocket.TServerSocket(self.listen_address, self.listen_port), TTransport.TFramedTransportFactory(), TBinaryProtocol.TBinaryProtocolFactory(), daemon=True, ) # Start Tanit server server.serve()
def server(saas, handler, host=None, port=SAAS_PORT, allowed_ips=None): processor = saas.Processor(handler) # transport = TServerSocket(host, port=SAAS_PORT) # transport = TSSLServerSocket(host, port=SAAS_PORT, certfile=SSL_CERT) sock = MySSLServerSocket(host, port=port, certfile=SSL_CERT, allowed_ips=allowed_ips) #tfactory = TTransport.TBufferedTransportFactory() tfactory = TTransport.TFramedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() #server = TServer.TSimpleServer(processor, sock, tfactory, pfactory) # which cannot deal with exception correctly server = TServer.TThreadedServer(processor, sock, tfactory, pfactory) #server = TServer.TThreadPoolServer(processor, sock, tfactory, pfactory) # which will hang and only be kill by signal 9 server.serve()
def _run_applications_server(self, server_port): # set handler to our implementation handler = ApplicationToWieraHandler(self) processor = ApplicationToWieraIface.Processor(handler) transport = TSocket.TServerSocket(port=server_port) tfactory = TTransport.TFramedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() # set server server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory, daemon=True) print '[Wiera] Starting applications server port:' + str(server_port) server.serve()
def run(self): """ Starts thrift server in a Daemon Returns: """ self.logger = logging.getLogger(__name__) config = config_loader.get(sys.argv[2]) handler = WorkerHandler(sys.argv[2], self.rundir) proc = Worker.Processor(handler) trans_svr = TSocket.TServerSocket(port=int(sys.argv[3]) + int(config["port"]["start"])) trans_fac = TTransport.TFramedTransportFactory() proto_fac = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TThreadPoolServer(proc, trans_svr, trans_fac, proto_fac) self.logger.info("Starting CaffeWorker Daemon") server.serve()
def set_factory(self): if settings.THRIFTSERVER_BUFFERED: self._tfactory = TTransport.TBufferedTransportFactory() if settings.THRIFTSERVER_ZLIB: self._tfactory = TZlibTransport.TZlibTransportFactory() if settings.THRIFTSERVER_FRAMED: self._tfactory = TTransport.TFramedTransportFactory() if settings.THRIFTSERVER_PROTOCOL == Protocol.BINARY: self._pfactory = TBinaryProtocol.TBinaryProtocolFactory() elif settings.THRIFTSERVER_PROTOCOL == Protocol.COMPACT: self._pfactory = TCompactProtocol.TCompactProtocolFactory() elif settings.THRIFTSERVER_PROTOCOL == Protocol.JSON: self._pfactory = TJSONProtocol.TJSONProtocolFactory() else: raise RuntimeError( "invalid configuration for THRIFTSERVER_PROTOCOL: {}".format( settings.THRIFTSERVER_PROTOCOL))
def run(host=HOST, port=PORT): try: # 注意确保模型参数文件存在,若不存在则先运行examples.py中的train方法 model = BayesSpamModel.import_model() # 线程池 server = TServer.TThreadPoolServer( MailQueryService.Processor(MailQueryServiceHandler(model)), # 注意py3的socket tcp通信默认采用tcpv6 TSocket.TServerSocket(host, port), TTransport.TFramedTransportFactory(), TCompactProtocol.TCompactProtocolFactory()) server.setNumThreads(3) server.serve() except Thrift.TException as e: logging.exception(repr(e))
def run(): if not readConf(): logService.info('read conf file is failed!') #print 'read conf file is failed!' return None # build news_infer_server if not hasValidPort(): logService.info("doesn't have valid port") #print "doesn't have valid port" return None handler = SameVideoHandler() valid_port = getPort() if valid_port == None: logService.info("doesn't have valid port") #print "doesn't have valid port" return None transport = TSocket.TServerSocket(port=valid_port) processor = SameVideoJudgeService.Processor(handler) #tfactory = TTransport.TBufferedTransportFactory() tfactory = TTransport.TFramedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() #server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory) #server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) #server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory) #server = TNonblockingServer.TNonblockingServer(processor, transport, tfactory, pfactory) server = TNonblockingServer.TNonblockingServer(processor, transport, pfactory) server.daemon = True server.setNumThreads(thread_num) #server.setNumWorkers(thread_num) print threading.current_thread() print('Starting the infer server...') server.serve() #logAll.error('done.') print('done')
def main(): parser = argparse.ArgumentParser() parser.add_argument('--port', type=int, help='Remote port') args = parser.parse_args() try: handler = SpheroHandler() processor = SpheroService.Processor(handler) transport = TSocket.TServerSocket(port=args.port) tfactory = TTransport.TFramedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) server.serve() except Exception as e: print(f"something went wrong: {e}")
def main(): global verbose global return_values global tracing global DEBUGGING arguments = docopt(__doc__) if DEBUGGING: print arguments verbose = arguments['--verbose'] return_values = arguments['--return-values'] buffered = arguments['--buffered'] tracing = not arguments['--no-trace'] if verbose and buffered: print 'Using buffered transport' client_type = arguments['<ThriftType.Client>'] base_module_name = client_type[:-7] if client_type.endswith( '.Client') else client_type base_module = __import__(base_module_name, globals(), locals(), ['Client', 'Processor']) client_type = base_module.Client processor_type = base_module.Processor with make_client(client_type, int(arguments['<client_port>']), arguments['--hostname'], buffered=buffered) as client: proxy = make_proxy(client) if verbose: for (attrname, spec) in get_service_methods(client): print attrname, getattr(proxy, attrname).__doc__ processor = processor_type(proxy) transport = TSocket.TServerSocket(port=int(arguments['<listen_port>'])) tfactory = TTransport.TBufferedTransportFactory( ) if buffered else TTransport.TFramedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) server.serve()
def run_forever(self): # set handler to our implementation handler = WieraTieraServerHandler(self) processor = WieraTieraServerIface.Processor(handler) transport = TSocket.TServerSocket(port=self.wiera_server_manager_port) tfactory = TTransport.TFramedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() # set server server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory, daemon=True) #set socket thread 20 min server.setNumThreads(32) print '[TSM] Tiera Server Manager is ready for Tiera Server port:' + str( self.wiera_server_manager_port) server.serve()
def main_thread(): # 1. 创建 Thrift Server 的 ServeSocket serverSocket = TSocket.TServerSocket(host=host, port=port) # serverSocket = TSocket.TServerSocket(None, port="9091") # 2. 创建 Thrift Server 的 Transport = 帧传输方式 transportFactory = TTransport.TFramedTransportFactory() # 3. 创建 Thrift Server 的 Protocol = 二进制传输协议 protocolFactory = TBinaryProtocol.TBinaryProtocolFactory() # 4. 创建 Thrift Server 提供的处理方法 handler = RecommendServiceHandler() # from message.api import MessageService processor = RecommendService.Processor(handler) # 5. 创建 Thrift Server, 指明如何处理,监听什么端口,传输方式,传输协议 # thriftServer = TServer.TSimpleServer(processor, # serverSocket, # transportFactory, # protocolFactory) thriftServer = TNonblockingServer.TNonblockingServer( processor, serverSocket, protocolFactory, protocolFactory) # 6. 启动 Thrift Server, 等待客户端的访问 print("Python Thrift Server start ...") thriftServer.serve() print("Python Thrift Server stop ...")
if str(cardNumber) in entry) #print('returning ' + str(len(log)) + ' entries') return log def insertTransaction(self, cardNumber, amount, entryMode, description): global TRANSACTION_LOG new_entry = (datetime.datetime.now() + datetime.timedelta(days=random.randint(0,15))).isoformat() + ',' + \ cardNumber + ',' + \ str(amount) + ',' + \ entryMode + ',' + \ description TRANSACTION_LOG.append(new_entry) print('[' + SERVER_PORT[0] + ':' + str(SERVER_PORT[1]) + '] posted ' + new_entry) return True if __name__ == '__main__': handler = TransactionHistoryDBHandler() processor = TransactionHistoryDB.Processor(handler) transport = TSocket.TServerSocket(host=SERVER_PORT[0], port=SERVER_PORT[1]) tfactory = TTransport.TFramedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TThreadedServer(processor, transport, tfactory, pfactory) print('[' + SERVER_PORT[0] + ':' + str(SERVER_PORT[1]) + ']' + ' Starting the TransactionHistoryDBServer...') server.serve() print('[' + SERVER_PORT[0] + ':' + str(SERVER_PORT[1]) + ']' + ' TransactionHistoryDBServer done.')
def __create_server(processor, ip, port): from ._gevent import GProcessPoolServer transport = TSocket.TServerSocket(ip, port) tfactory = TTransport.TFramedTransportFactory() pfactory = TCompactProtocol.TCompactProtocolFactory() return GProcessPoolServer(processor, transport, tfactory, pfactory)
def main(cfg): if cfg.unix: if cfg.addr == "": sys.exit("invalid listener unix domain socket: {}".format(cfg.addr)) else: try: (host, port) = cfg.addr.rsplit(":", 1) port = int(port) except ValueError: sys.exit("invalid listener address: {}".format(cfg.addr)) if cfg.response == "success": handler = SuccessHandler() elif cfg.response == "idl-exception": handler = IDLExceptionHandler() elif cfg.response == "exception": # squelch traceback for the exception we throw logging.getLogger().setLevel(logging.CRITICAL) handler = ExceptionHandler() else: sys.exit("unknown server response mode {0}".format(cfg.response)) processor = Example.Processor(handler) if cfg.service is not None: # wrap processor with multiplexor multi = TMultiplexedProcessor.TMultiplexedProcessor() multi.registerProcessor(cfg.service, processor) processor = multi if cfg.unix: transport = TSocket.TServerSocket(unix_socket=cfg.addr) else: transport = TSocket.TServerSocket(host=host, port=port) if cfg.transport == "framed": transport_factory = TTransport.TFramedTransportFactory() elif cfg.transport == "unframed": transport_factory = TTransport.TBufferedTransportFactory() elif cfg.transport == "header": if cfg.protocol == "binary": transport_factory = THeaderTransport.THeaderTransportFactory( THeaderTransport.T_BINARY_PROTOCOL) elif cfg.protocol == "compact": transport_factory = THeaderTransport.THeaderTransportFactory( THeaderTransport.T_COMPACT_PROTOCOL) else: sys.exit("header transport cannot be used with protocol {0}".format(cfg.protocol)) else: sys.exit("unknown transport {0}".format(cfg.transport)) if cfg.protocol == "binary": protocol_factory = TBinaryProtocol.TBinaryProtocolFactory() elif cfg.protocol == "compact": protocol_factory = TCompactProtocol.TCompactProtocolFactory() elif cfg.protocol == "json": protocol_factory = TJSONProtocol.TJSONProtocolFactory() else: sys.exit("unknown protocol {0}".format(cfg.protocol)) print( "Thrift Server listening on {0} for {1} {2} requests".format( cfg.addr, cfg.transport, cfg.protocol)) if cfg.service is not None: print("Thrift Server service name {0}".format(cfg.service)) if cfg.response == "idl-exception": print("Thrift Server will throw IDL exceptions when defined") elif cfg.response == "exception": print("Thrift Server will throw Thrift exceptions for all messages") server = TServer.TThreadedServer(processor, transport, transport_factory, protocol_factory) try: server.serve() except KeyboardInterrupt: print
def start_gstream(module, handler_class, addr, max_conn=1000, framed=False, max_process=1, stop_callback=None): global service handler = handler_class() processor = module.Processor(handler) if framed: tfactory = TTransport.TFramedTransportFactory() else: tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() def signal_master_handler(signum, frame): global service log.info( "signal %d catched, server will exit after all request handled", signum) for i in service: i.terminate() def signal_worker_handler(signum, frame): global service log.info("worker [%d] will exit after all request handled", os.getpid()) service.close() if stop_callback: stop_callback() def server_forever(listener): global service log.info('worker [%d] start', os.getpid()) service = GStreamServer(listener, processor=processor, inputTransportFactory=tfactory, inputProtocolFactory=pfactory, spawn=max_conn) signal.signal(signal.SIGTERM, signal_worker_handler) service.start() gevent.wait() log.info('worker [%d] exit', os.getpid()) listener = GStreamServer.get_listener(addr, family=socket.AF_INET) log.info('server start at %s:%d pid:%d', addr[0], addr[1], os.getpid()) if max_process == 1: server_forever(listener) else: service = [ multiprocessing.Process(target=server_forever, args=(listener, )) for i in range(max_process) ] for i in service: i.start() signal.signal(signal.SIGTERM, signal_master_handler) for i in service: i.join()
def sendIpAddress(self, ip): # send ip to DSS logger.info("[THRIFT SERVER] Receiving IP from MMT. IP:{}".format(ip)) splitted = ip.rstrip().split(':') if len(splitted) == 1: # ip do not has port url = splitted[0] port = '' else: # ip do has port url = splitted[0] port = splitted[1] updateips.in_ipsgv({"url": url, "port": port}) if __name__ == "__main__": # run thrift server handler = DssServiceHandler() proc = DssService.Processor(handler) trans_svr = TSocket.TServerSocket(port=globals.MMT_SERVER_PORT) #trans_fac = TTransport.TBufferedTransportFactory() trans_fac = TTransport.TFramedTransportFactory() proto_fac = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(proc, trans_svr, trans_fac, proto_fac) logger.info("[THRIFT SERVER] Started in port {}".format( globals.MMT_SERVER_PORT)) print("[THRIFT SERVER] Started in url {}, port {}".format( globals.MMT_SERVER_URL, globals.MMT_SERVER_PORT)) server.serve()