grpc added standard connect/close along with context connect
This commit is contained in:
parent
9c973249e8
commit
d9ae7d5c34
1 changed files with 15 additions and 6 deletions
|
@ -23,6 +23,7 @@ class CoreApiClient(object):
|
||||||
def __init__(self, address="localhost:50051"):
|
def __init__(self, address="localhost:50051"):
|
||||||
self.address = address
|
self.address = address
|
||||||
self.stub = None
|
self.stub = None
|
||||||
|
self.channel = None
|
||||||
|
|
||||||
def create_session(self):
|
def create_session(self):
|
||||||
return self.stub.CreateSession(core_pb2.CreateSessionRequest())
|
return self.stub.CreateSession(core_pb2.CreateSessionRequest())
|
||||||
|
@ -320,21 +321,29 @@ class CoreApiClient(object):
|
||||||
request.data = data
|
request.data = data
|
||||||
return self.stub.OpenXml(request)
|
return self.stub.OpenXml(request)
|
||||||
|
|
||||||
@contextmanager
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
channel = grpc.insecure_channel(self.address)
|
self.channel = grpc.insecure_channel(self.address)
|
||||||
|
self.stub = core_pb2_grpc.CoreApiStub(self.channel)
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
if self.channel:
|
||||||
|
self.channel.close()
|
||||||
|
self.channel = None
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def context_connect(self):
|
||||||
try:
|
try:
|
||||||
self.stub = core_pb2_grpc.CoreApiStub(channel)
|
self.connect()
|
||||||
yield channel
|
yield
|
||||||
finally:
|
finally:
|
||||||
channel.close()
|
self.close()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
xml_file_name = "/tmp/core.xml"
|
xml_file_name = "/tmp/core.xml"
|
||||||
|
|
||||||
client = CoreApiClient()
|
client = CoreApiClient()
|
||||||
with client.connect():
|
with client.context_connect():
|
||||||
if os.path.exists(xml_file_name):
|
if os.path.exists(xml_file_name):
|
||||||
print("open xml: {}".format(client.open_xml(xml_file_name)))
|
print("open xml: {}".format(client.open_xml(xml_file_name)))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue