def connection_to_lb(self): if self.unix_socket: info_logger.info("Prepare open a socket to lb: %s, pid: %s", self.unix_socket, self.pid) else: info_logger.info("Prepare open a socket to lb: %s:%s, pid: %s", self.host, self.port, self.pid) # 1. 创建一个到lb的连接,然后开始读取Frame, 并且返回数据 socket = TSocket(host=self.host, port=self.port, unix_socket=self.unix_socket) try: if not socket.isOpen(): socket.open() socket.setTimeout(5000) # 出现异常,会自己重启 except TTransportException: info_logger.info("Sleep %ds for another retry, pid: %s", self.reconnect_interval, self.pid) time.sleep(self.reconnect_interval) print_exception(info_logger) if self.reconnect_interval < 4: self.reconnect_interval *= 2 return # 2. 连接创建成功 self.reconnect_interval = 1 self.socket = socket # 每次建立连接都重新构建 self.queue = gevent.queue.Queue() self.connection_ok = True info_logger.info("Begin request loop....") # 3. 在同一个transport上进行读写数据 transport = TCyFramedTransportEx(socket) # # 关注 transport的接口: # flush_frame_buff # read_frame # g1 = gevent.spawn(self.loop_reader, transport, self.queue) g2 = gevent.spawn(self.loop_writer, transport, self.queue) g3 = gevent.spawn(self.loop_hb_detect, transport) gevent.joinall([g1, g2, g3]) # 4. 关闭连接 try: # 什么情况下会关闭呢? 连接断开了, print time.strftime(ISOTIMEFORMAT, time.localtime()), "Trans Closed, queue size: ", self.queue.qsize(), ", pid: ", self.pid self.queue = None self.socket = None transport.close() # 关闭transport(而且transport也不会继续复用) except: print_exception(info_logger) pass
def test_write_flush(self): """ py.test test/test_framed_buffer.py::FramedBufferTest::test_write_flush -s """ buf = TMemoryBuffer() transport = TCyFramedTransport(buf) transport.write("abcdef") transport.flush() print "Framed Output: ", ["%03d" % ord(i) for i in buf.getvalue()] # MemoryBuffer作为FrameBuffer来使用 buf2 = TCyMemoryBuffer() buf2.prepare_4_frame() buf2.write("abcdef") buf1 = TMemoryBuffer() tran1 = TCyFramedTransportEx(buf1) tran1.flush_frame_buff(buf2) print "Framed Output: ", ["%03d" % ord(i) for i in buf1.getvalue()] buf1.reset() tran1 = TCyFramedTransportEx(buf1) mem_trans = tran1.read_frame() print "ReadFrame: ", mem_trans value = mem_trans.getvalue() print "Value: ", value, ["%03d" % ord(i) for i in value]
def connection_to_lb(self): if self.unix_socket: info_logger.info("Prepare open a socket to lb: %s, pid: %s", self.unix_socket, self.pid) else: info_logger.info("Prepare open a socket to lb: %s:%s, pid: %s", self.host, self.port, self.pid) # 1. 创建一个到lb的连接,然后开始读取Frame, 并且返回数据 socket = TSocket(host=self.host, port=self.port, unix_socket=self.unix_socket) try: if not socket.isOpen(): socket.open() socket.setTimeout(5000) # 出现异常,会自己重启 except TTransportException: info_logger.info("Sleep %ds for another retry, pid: %s", self.reconnect_interval, self.pid) time.sleep(self.reconnect_interval) print_exception(info_logger) if self.reconnect_interval < 4: self.reconnect_interval *= 2 return # 2. 连接创建成功 self.reconnect_interval = 1 self.socket = socket # 每次建立连接都重新构建 self.queue = gevent.queue.Queue() self.connection_ok = True info_logger.info("Begin request loop....") # 3. 在同一个transport上进行读写数据 transport = TCyFramedTransportEx(socket) # # 关注 transport的接口: # flush_frame_buff # read_frame # g1 = gevent.spawn(self.loop_reader, transport, self.queue) g2 = gevent.spawn(self.loop_writer, transport, self.queue) g3 = gevent.spawn(self.loop_hb_detect, transport) gevent.joinall([g1, g2, g3]) # 4. 关闭连接 try: # 什么情况下会关闭呢? 连接断开了, print time.strftime(ISOTIMEFORMAT, time.localtime( )), "Trans Closed, queue size: ", self.queue.qsize( ), ", pid: ", self.pid self.queue = None self.socket = None transport.close() # 关闭transport(而且transport也不会继续复用) except: print_exception(info_logger) pass