0

In gRPC python, the Service-class and the Serve method are always in the same file, why? for example - the service class - link and the serve() - link

Similarly - service-class and serve()

I am new to python and grpc. In my project I wrote the serve method in different file importing the service-class, the server seems started but when I invoke it from client code (postman) it doesn't work

Here is my code - ems_validator_service.py contains the service class and main.py has the serve() method

File: ems_validator_service.py -

from validator.src.grpc import ems_validator_service_pb2
from validator.src.grpc.ems_validator_service_pb2_grpc import EmsValidatorServiceServicer


class EmsValidatorServiceServicer(EmsValidatorServiceServicer):
    def Validate(self, request, context):
        # TODO: logic to validate
        return ems_validator_service_pb2.GetStatusResponse(
            validation_status=ems_validator_service_pb2.VALIDATION_STATUS_IN_PROGRESS)

    def GetStatus(self, request, context):
        # TODO: logic to get actual status
        return ems_validator_service_pb2.GetStatusResponse(
            validation_status=ems_validator_service_pb2.VALIDATION_STATUS_IN_PROGRESS)

File: main.py -

from validator.src.grpc.ems_validator_service_pb2_grpc import (
    EmsValidatorServiceServicer,
    add_EmsValidatorServiceServicer_to_server
)
from concurrent import futures
import grpc


def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    add_EmsValidatorServiceServicer_to_server(EmsValidatorServiceServicer(), server)
    server.add_insecure_port('localhost:50051')  # todo change it
    server.start()
    server.wait_for_termination()


if __name__ == "__main__":
    serve()

For the above code I can't invoke the rpc. but If I move the serve method to the ems_validator_service.py file and call that method from main.py then it works fine. Not sure if it is a python thing or gRPC thing?

The error I get from client.py -

  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/grpc/_channel.py", line 946, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/grpc/_channel.py", line 849, in _end_unary_response_blocking
    raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
        status = StatusCode.UNIMPLEMENTED
        details = "Method not implemented!"
        debug_error_string = "UNKNOWN:Error received from peer ipv6:%5B::1%5D:50051 {created_time:"2022-10-19T22:10:18.898439-07:00", grpc_status:12, grpc_message:"Method not implemented!"}"
>

As already mentioned, the same client works fine if I move the above serve() method to the service class

Hasan
  • 1
  • 1

1 Answers1

0

These are just simple examples. It's totally fine to call serve() from a different file.

Richard Belleville
  • 1,445
  • 5
  • 7
  • It is not working for me when I am calling serve() from a different file. In the above example serve is in main.py and ServiceServicer is in another file, it is not working in this way. not getting response from the server (I am calling it from postman) – Hasan Oct 19 '22 at 21:35
  • And the error message is? – Richard Belleville Oct 20 '22 at 01:31
  • Updated the post with error. Basically the error is - grpc_status:12, grpc_message:"Method not implemented!"} – Hasan Oct 20 '22 at 05:16
  • Unimplemented is more or less a 404. Are you sure that `add_EmsValidatorServiceServicer_to_server` is still being called properly? – Richard Belleville Oct 20 '22 at 18:25
  • Ahh, darn it, was stuck with such a silly mistake, yeah, I was calling `add_EmsValidatorServiceServicer_to_server` with the unimplemented version of `EmsValidatorServiceServicer()`. And when I move the same code/method to file: `ems_validator_service.py` the implemented version of it was getting run (as the implementation was in same file) thus it was working. In my head I was like how come same code works in one place and not in another place. – Hasan Oct 20 '22 at 20:36