Scala to Python using gRPC

43 views
Skip to first unread message

S SATHISH BABU

unread,
Jan 30, 2019, 1:58:37 AM1/30/19
to grpc.io
Hello,
    I am making a communication between my Scala gRPC client and python gRPC server. My issue is when I make a blocking call from scala client, my request is handled. But in case of asynchronous stub, the request is not delivered at all. Can anyone help me out with this?.

Python gRPC server code

from gRPC2 import message_pb2, message_pb2_grpc
import grpc
from concurrent import futures
import time


class HandlerServicer(message_pb2_grpc.HandlerServicer):

    def request_handler(self, request, context):
        print('Received request:', request, request.request_message)
        response = 'Reponse'
        time.sleep(10)
        return message_pb2.Response(response_message=response)


handler_server = grpc.server(futures.ThreadPoolExecutor())
message_pb2_grpc.add_HandlerServicer_to_server(HandlerServicer(), handler_server)
handler_server.add_insecure_port("[::]:{}".format(5000))

print('Starting server...')
handler_server.start()
try:
    while True:
        time.sleep(60*60*60)
except KeyboardInterrupt:
    print('Stopping server...')
    handler_server.stop(0)

Scala gRPC client code:

import io.grpc.ManagedChannelBuilder
import message._

import scala.concurrent.Future
import scala.concurrent.ExecutionContext

object HandlerClient extends App {

  val channel = ManagedChannelBuilder.forAddress("localhost", 5000).usePlaintext().build()
  val request = Request("Request")
  implicit val ec = ExecutionContext.global

  // Making a Async call
  val stub = HandlerGrpc.stub(channel)
  val future = stub.requestHandler(request)
future onComplete println
}


Proto file:

syntax = "proto3";

service Handler{
rpc request_handler(Request) returns (Response){}
}

message Request{
string request_message = 1;
}

message Response{
string response_message = 1;
}

Thanks,
S Sathish Babu

Lidi Zheng

unread,
Jan 30, 2019, 1:07:04 PM1/30/19
to S SATHISH BABU, grpc.io
Hello,

The Scala client is exiting too quick that the future is not yet completed. You may use "Await.ready" or "Await.result" to explicitly block the program.

Cheers,
Lidi Zheng

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/4ce6fa34-b5a5-49d0-9296-b6c25cdc33ae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

S SATHISH BABU

unread,
Jan 30, 2019, 11:07:28 PM1/30/19
to grpc.io
Hello Lidi Zheng,

Thanks for the response. As you said,using "Await.ready" or "Await.block" will block the program and return me the result. In my case, I want it to execute asynchronously. There should be no blocking. Let the server return the response when it has finished computing. Is that possible?

Regards,
S Sathish Babu

Lidi Zheng

unread,
Jan 30, 2019, 11:40:30 PM1/30/19
to S SATHISH BABU, grpc.io
Hi S Sathish Babu,

In that case, you want to keep your main thread alive after you dispatched your premises. I presume in Scala the "Future" works fine with "Thread.sleep"? If so, you may block your main thread by explicitly calling "Thread.sleep" at the end of your main function, and your premises will be executed asynchronously.

Cheers,
Lidi Zheng

S SATHISH BABU

unread,
Jan 30, 2019, 11:59:28 PM1/30/19
to grpc.io
Hello Lidi Zheng,

I couldn't get your point.

Regards,
S Sathish Babu

S SATHISH BABU

unread,
Jan 31, 2019, 12:04:34 AM1/31/19
to grpc.io
Hello Lidi Zheng,

Actually, the python server is not receiving any request when using a asynhronous stub. But in case of blocking stub, it receives the request. Does it have to do anything with the executors at the python side?.

Regards,
S Sathish Babu

On Wednesday, 30 January 2019 23:37:04 UTC+5:30, Lidi Zheng wrote:
Reply all
Reply to author
Forward
0 new messages