本文整理汇总了Python中thrift.transport.TSocket.TSocket.isOpen方法的典型用法代码示例。如果您正苦于以下问题:Python TSocket.isOpen方法的具体用法?Python TSocket.isOpen怎么用?Python TSocket.isOpen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类thrift.transport.TSocket.TSocket
的用法示例。
在下文中一共展示了TSocket.isOpen方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: connection_to_lb
# 需要导入模块: from thrift.transport.TSocket import TSocket [as 别名]
# 或者: from thrift.transport.TSocket.TSocket import isOpen [as 别名]
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