"""Utilities for building the thrift transport client."""
import contextlib
from thrift.protocol import TBinaryProtocol
from thrift.transport import TSocket, TTransport
@contextlib.contextmanager
def client_transport_maker(host, port, rpc_type):
"""Context manager to build the thrift transport client."""
transport = TSocket.TSocket(host=host, port=port)
tframed_transport = TTransport.TFramedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(tframed_transport)
client = rpc_type.Client(protocol)
try:
transport.open()
yield client
finally:
transport.close()
使用context manager创建thrift client的统一入口
最新推荐文章于 2020-12-21 10:35:44 发布