Python gRPC cancel unary-stream call from client side

23 views
Skip to first unread message

Mark Nuttall-Smith

unread,
Mar 15, 2019, 2:54:15 AM3/15/19
to grpc.io
Hi,

Using Python gRPC, I would like to be able to cancel a long-running unary-stream call from the client side, when a `threading.Event` is set.

    def application(stub: StreamsStub, event: threading.Event):
        stream
= stub.Application(ApplicationStreamRequest())
       
try:
           
for resp in stream:
               
print(resp)
       
except grpc.RpcError as e:
           
print(e)

For the time being I am cancelling the stream using the `channel.close()` method, but of course this closes all connections rather than just this stream.

Could someone suggest how I can use the event to cancel the stream iterator? 

Thanks, Mark

Lidi Zheng

unread,
Mar 15, 2019, 3:26:59 AM3/15/19
to Mark Nuttall-Smith, grpc.io
Can you try “stream.cancel()”? The “cancel()” method is available for both client context and server context. See class grpc.RpcContext in 

--
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/2b7ed001-d5b9-452b-b4dd-fbcb905687b3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark Nuttall-Smith

unread,
Mar 15, 2019, 6:36:03 AM3/15/19
to grpc.io
Easy, thanks very much.

I should have looked at the code for the _Rendezvous object, rather than getting frustrated with the documentation. 
Reply all
Reply to author
Forward
0 new messages