My first time playing around with Python and grpc. I have been troubleshooting for a while now and cant seem to get past the below issue.
I am getting the following error by running: venv/bin/python placeholder.py (excuse the file name Im still sandboxing).
Error:
Traceback (most recent call last):
File "placeholder.py", line 50, in <module>
client = BitcClient()
File "placeholder.py", line 20, in __init__
self.stub = pb2_grpc.BranchesStub(self.channel)
File "/Users/me/PycharmProjects/app/infrastructure/v3/branches_pb2_grpc.py", line 18, in __init__
self.ListBranches = channel.unary_unary(
AttributeError: 'NoneType' object has no attribute 'unary_unary'
Here are the specs of my setup:
Specs:
certifi==2022.6.15
charset-normalizer==2.1.1
grpcio==1.39.0
grpcio-tools==1.39.0
idna==3.3
protobuf==3.20.1
requests==2.28.1
six==1.16.0
urllib3==1.26.12
(venv) % venv/bin/python --version
Python 3.8.9
Here is my client code. The server code I do not have access to as it is built by another team.
Client code:
import grpc
import app.infrastructure.v3.branches_pb2 as pb2
import app.infrastructure.v3.branches_pb2_grpc as pb2_grpc
class Client(object):
def __init__(self):
self.server = 'api.domain.com:443'
# instantiate a channel
self.channel = grpc.insecure_channel('{}'.format(self.server))
self.channel = grpc.channel_ready_future(self.channel).result(timeout=3)
# bind the client and the server
self.stub = pb2_grpc.BranchesStub(self.channel)
def get_branches(self, message):
message = pb2.ListBranchesResponse(message=message)
print(f'{message}')
return self.stub.ListBranches(message)
if __name__ == '__main__':
api_client = Client()
result = api_client()
Below is relevant code that was generated from .proto files:
Proto generated code:
class BranchesStub(object):
"""Operations to manage branches and their releation to projects.
"""
def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.ListBranches = channel.unary_unary(
'/app.infrastructure.v3.Branches/ListBranches',
request_serializer=dot_app_dot_infrastructure_dot_v3_dot_branches__pb2.ListBranchesRequest.SerializeToString,
response_deserializer=dot_app_dot_infrastructure_dot_v3_dot_branches__pb2.ListBranchesResponse.FromString,
)
...
class BranchesServicer(object):
"""Operations to manage branches and their releation to Console projects.
"""
def ListBranches(self, request, context):
"""List all created branches (and their status) for the specified project
"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
Help would be greatly appreciated!
Turns out issue was caused by this line:
self.channel = grpc.channel_ready_future(self.channel).result(timeout=3)
This line worked when I used it during another unrelated client.py file. Not sure why its failing now but at least my code seems to work now after removing it.