2016-07-22 84 views
1

在一個Python模塊中,我正在做一些東西。在做這些事情的過程中,我創建了一個Thrift連接。問題在於連接開始後,程序停滯在網絡邏輯中。 (即阻塞)。非阻塞服務器Apache Thrift Python

在模塊A,我有:

stuff = "do some stuff" 
network.ConnectionManager(host, port, ...) 
stuff = "do more stuff" # not getting to this point 

在網絡......

ConnectionManager.start_service_handler() 
def start_service_handler(self): 
     handler = ServiceHandler(self) 
     processor = Service.Processor(handler) 
     transport = TSocket.TServerSocket(port=self.port) 
     tfactory = TTransport.TBufferedTransportFactory() 
     pfactory = TBinaryProtocol.TBinaryProtocolFactory() 
     # server = TServer.TThreadedServer(processor, transport, tfactory, pfactory) 
     server = TNonblockingServer(processor, transport, tfactory, pfactory) 
     logger().info('starting server...') 
     server.serve() 

我嘗試這一點,但在模塊A尚碼作爲連接代碼開始不會馬上繼續。

我以爲TNonblockingServer會做的伎倆,但不幸的是沒有。

回答

2

代碼塊在server.serve()這是由Thrift支持的所有目標語言設計的。通常的用例是運行像這樣的(僞代碼)的服務器:

init server 
setup thrift protocol/tramsport stack 
server.serve() 
shutdown code 

的「非阻塞」並不指server.serve()通話,而採取實際的客戶端調用的代碼。使用TSimpleServer,服務器一次只能處理一個呼叫。相反,TNonblockingServerdesigned to accept a number of connections in parallel。結論:如果你想運行一個Thrift服務器並且還有其他一些工作要做並行,或者需要在程序運行期間動態地啓動和停止服務器,那麼你將需要另一個線程來實現它。