Python GRPC server streaming: How to handle client side termination correctly?

27 views
Skip to first unread message

Jonathan Chung

unread,
Jun 7, 2024, 2:28:18 PMJun 7
to grpc.io
I'm new to GRPC and am unsure how to handle server streaming correctly if a client terminates.

Currently I have server streaming which gets content from a LifoQueue and streams the content of the queue to some connected client. Each client has an id (in request.id) which gets the correct queue for this client:

...
def addMsg(self,id, msg): # producer
    queues[id].put(msg)

def stream(self, request, context): # consumer
       queues[request.id] = LifoQueue()
        while True:
            message = queues[request.id].get(block=True)
            yield message
...
The stream method gets called from the client - however if the client terminates, can I be sure that the server side also terminates this call eventually or does this need to be managed differently?

Jonathan Chung

unread,
Jun 7, 2024, 3:14:11 PMJun 7
to grpc.io
After some research, I found out more about the semantics of yield. As far as I understand, yield makes the function return a generator which I believe is like an iterator. So the question would be if GRPC/GC removes the reference to the iterator once the call terminates e.g due to a client side outrage. 
Reply all
Reply to author
Forward
0 new messages