Python thrift rpc TProcessPoolServer 原生的多进程服务模式

以前通过多线程每个线程中再启进程的模式实现了多进程的服务模式。
今天发现其实进程池的模型是自带的。
这个Server在一个单独的文件中,与其它Server不在一起,不看代码不太容易发现。

示例代码

import sys, glob  
sys.path.append('./gen-py')

from WormHoleRPCService import WormHoleRPCService 
from WormHoleRPCService.ttypes import *  


from thrift.transport import TSocket  
from thrift.transport import TTransport  
from thrift.protocol import TBinaryProtocol  
from thrift.server import TProcessPoolServer  


from time import sleep
import multiprocessing


def work_hi(pipe) :
    pass

def work_add(pipe ,  a , b) :
    pass

def work_getMap(a) :
    pass


def work_getList(a , b , c , d) :
    pass
    i = 0
    while True :
        i += 1 ;
        if i > 30000000 :
            break
    res = [{a : 'tom' , b:'jim'} , {c : 'john' , d:'kobe'} ]
    return res



class WormHoleRPCServiceHandler:  
  def __init__(self):  
    self.log = {}  

  def hi(self):  
    print 'hi'

  def add(self , a , b) :
    return a + b 


  def getList(self , a , b , c , d) :
    return work_getList(a , b , c , d)


  def getMap(self , a ) :
    return a



rpc_handler = WormHoleRPCServiceHandler()  
processor = WormHoleRPCService.Processor(rpc_handler)  
transport = TSocket.TServerSocket(port=9090)  
tfactory = TTransport.TBufferedTransportFactory()  
pfactory = TBinaryProtocol.TBinaryProtocolFactory()  

# You could do one of these for a multithreaded server  
server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory)  

print 'Starting the server...'  
server.setNumWorkers(5)
server.serve()  
print 'done.'  

你可能感兴趣的:(python,thrift,server,多进程,杂谈)