def get_new_client(self): """ 轮询在每个ip:port的连接池中获取连接(线程安全) 从当前队列右侧取出ip:port信息,获取client 将连接池对象放回到当前队列的左侧 请求或连接超时时间,默认30秒 :return: """ with self.lock: if self.pool_size < self.maxActive: try: ip = self.load_balance_queue.pop() except IndexError: raise CTECThriftClientError('没有可用的服务提供者列表!') if ip: self.load_balance_queue.appendleft(ip) # 创建新的thrift client t_socket = TSocket(ip.split(':')[0], int(ip.split(':')[1]), socket_timeout=1000 * self.socket_timeout) proto_factory = TBinaryProtocolFactory() trans_factory = TBufferedTransportFactory() transport = trans_factory.get_transport(t_socket) protocol = proto_factory.get_protocol(transport) transport.open() client = TClient(self.service, protocol) self.pool_size += 1 return client else: return None
def __init__(self): super(ThriftLocust, self).__init__() socket = TSocket('127.0.0.1', 9999) proto_factory = TBinaryProtocolFactory() trans_factory = TBufferedTransportFactory() transport = trans_factory.get_transport(socket) protocol = proto_factory.get_protocol(transport) transport.open() hash_thrift = thriftpy.load("hash.thrift", module_name="hash_thrift") self.client = ThriftClient(hash_thrift.HashService, protocol)