Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cancel a Read() call for grpc::ClientReader in C++?

Tags:

c++

c++11

grpc

I have code that requests a stream of data from a grpc::Server with a grpc::ClientReader. The main loop looks like below, and runs in a seperate task. I need to shutdown task when destructor, but the Read() method is blocking. There doesn't seem to be anything I can do to mcReader to get it to stop blocking. I would rather not use deadline, because data isn't completely periodic, and making it big enough to do antyhign would still block a long time. What should I do?

while (mcReader->Read(&dataProductWrapper) && meTasksRunning) 
{
      // Do some work.
}
like image 483
CptanPanic Avatar asked Oct 18 '25 01:10

CptanPanic


1 Answers

So looking around I figured it out. Using the grpc::ClientContext that gets passed into RPC call, you can call grpc::ClientContext::TryCancel() you can cancel from another thread.

like image 52
CptanPanic Avatar answered Oct 20 '25 16:10

CptanPanic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!