how to close a stream from a client (Python)

3,352 views
Skip to first unread message

flazz...@gmail.com

unread,
Oct 27, 2016, 1:54:50 PM10/27/16
to grpc.io
Hi,

In a Python grpc client & server. Given some unary-stream rpc call:

call = stub.SomeUnaryStream(input_msg)
xs = itertools.islice(call, 2) # take two
call.cancel() # this doesn't seem to signal the server

How can I signal to the server that the call is over, and resources can be freed? The call is cancelled on the client side, but I get no notification until a GeneratorExit is thrown during a subsequent call. I assume the server-side GC is cleaning up and closing the generator. I made a golang client to see if the CloseSend method would signal but the behavior is similar.

Am I missing something here? or is the api incomplete?

-Franco

Ken Payson

unread,
Oct 27, 2016, 2:23:25 PM10/27/16
to flazz...@gmail.com, grpc.io
In order to cancel a call from Python, you need to use the asynchronous API.

It should look something like this:

call_future = stub.SomeUnaryStream.future(input_msg)
call_future.cancel()

Note that if you have a long running server process, you will need to explicitly check if the call has been cancelled from the server.



--
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+unsubscribe@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/185a533b-da4d-4cf7-8f26-4e23b59f6439%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

flazz...@gmail.com

unread,
Oct 27, 2016, 3:49:27 PM10/27/16
to grpc.io, flazz...@gmail.com
Ken, Thanks for the tip. It didn't work exactly as describe but you pointed me in the right direction for a solution.

UnaryStream calls don't have future, just __call__. butt checking the cancellation state from the server side is what I needed.

I ended up checking context.is_active() in a separate thread on the server. That thread signals the cleanup when ,is_active() returns False.

On Thursday, October 27, 2016 at 1:23:25 PM UTC-5, Ken Payson wrote:
In order to cancel a call from Python, you need to use the asynchronous API.

It should look something like this:

call_future = stub.SomeUnaryStream.future(input_msg)
call_future.cancel()

Note that if you have a long running server process, you will need to explicitly check if the call has been cancelled from the server.


On Thu, Oct 27, 2016 at 10:54 AM, <flazz...@gmail.com> wrote:
Hi,

In a Python grpc client & server. Given some unary-stream rpc call:

call = stub.SomeUnaryStream(input_msg)
xs = itertools.islice(call, 2) # take two
call.cancel() # this doesn't seem to signal the server

How can I signal to the server that the call is over, and resources can be freed? The call is cancelled on the client side, but I get no notification until a GeneratorExit is thrown during a subsequent call. I assume the server-side GC is cleaning up and closing the generator. I made a golang client to see if the CloseSend method would signal but the behavior is similar.

Am I missing something here? or is the api incomplete?

-Franco

--
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.

Nathaniel Manista

unread,
Nov 1, 2016, 8:27:18 PM11/1/16
to flazz...@gmail.com, grpc.io
On Thu, Oct 27, 2016 at 12:49 PM, <flazz...@gmail.com> wrote:
I ended up checking context.is_active() in a separate thread on the server. That thread signals the cleanup when ,is_active() returns False.

Polling the is_active method is not intended to be necessary for this case - how about registering a callback with the ServicerContext's add_callback method?
-Nathaniel

flazz...@gmail.com

unread,
Nov 2, 2016, 6:13:42 PM11/2/16
to grpc.io, flazz...@gmail.com
Thanks Nathaniel, exactly what I needed!
Reply all
Reply to author
Forward
0 new messages