I have a bi-directional gRPC that writes to a file and returns responses. 
Protobuf is defined roughly like this:


rpc Write(stream WriteRequest) returns (stream WriteResponse);

message FileData {
    int64 offset = 1;
    bytes data = 2;

message WriteRequest {
    repeated FileData extents = 1;

message WriteResponse {
    int64 throttle = 1;

The issue I'm running into is that I need to run special logic on the last 
request in the iterator before returning a response (e.g. flush buffers). 
The only way I can think to do that is to peek ahead in the iterator, but 
iterator.next() blocks forever if I haven't returned a response since the 
last time I called it.


def Write(self, request_iterator, context):        
    request = request_iterator.next()
    next_request = request

    while True:
        request = next_request            
        for extent in request.extents:
            # Write data

        # This call blocks forever
        next_request = request_iterator.next()            
        if not next_request:
            # Do important stuff before client unblocks
            yield WriteResponse(throttle=0)
            yield WriteResponse(throttle=0)

All I'm really trying to accomplish is running some logic at the very end 
of the RPC before the client unblocks. If I was using the Java 
implementation I would have access to ResponseObserver.onCompleted() but it 
seems like with Python, RPC completion is just implied through yield-ing 
the response that corresponds with the last request in the request iterator.

Is there some way to do this?

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+unsubscr...@googlegroups.com.
To post to this group, send email to grpc-io@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit 
For more options, visit https://groups.google.com/d/optout.

Reply via email to