C++ Async Example ( how to make client unblock from Next )

881 views
Skip to first unread message

Nid Raj

unread,
Apr 24, 2016, 3:12:13 PM4/24/16
to grpc.io

Using:

protobuf-3.0.0

grpc release 0.12

Looking at the helloworld greeter async example.  I'm able to build and run the client and server example.
My doubt is around unblocking the client side.   If I modify the server and put a sleep (of 10sec) at the processing
side of the RPC ( in the status_ == PROCESS ) the client side blocks on the Next api call.  I'm need to make
client side unblock (ie just make the RPC call and ignore the reply / ie. the block on Next ).

Few things I tried:

1. Removed the Finish, Next (attempt to ignore the reply )
    
I observe an error on the client side: 1 metadata elements were leaked, and the server never receives anything.

2. Attempted to move the Next into a thread.

At the time of connection, I spawned off a thread that sits in a loop on the Next.  cq is global

    void *got_tag;
    bool ok = false;

    while (1) {
       cq.Next(&got_tag,&ok);
    }

Observation is same as #1  ( server doesn't get anything and client gives leaked error ),  I also tried spawning thread
after the RPC call (ie where Next was originally getting called ) same issue.

Wondering if I'm missing something ? My requirements is make the async RPC call and have client side completely unblocked
to continue processing other things / requests.

thanks.

micha...@gmail.com

unread,
May 3, 2016, 6:02:10 PM5/3/16
to grpc.io
I'm actually in the same boat.  If anyone finds a solution for this, please post back.

Thanks in advance.

Yang Gao

unread,
May 3, 2016, 6:25:18 PM5/3/16
to Nid Raj, grpc.io
I think you can use your option 2 and call Next in a separate thread. In the main thread you can send the rpc, call Finish with a tag. When you get the tag back from Next, you can just do clean-up. The cq can be shared by multiple rpcs.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/01c0f1c6-9fa1-4185-85a4-4e57ba4826e7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

micha...@gmail.com

unread,
May 3, 2016, 6:35:02 PM5/3/16
to grpc.io, nid...@gmail.com
I've played around with that option, but haven't gotten it to work correctly.  It works fine when I call it from the main thread, but as soon as I fork a thread, ok is coming back as false:

void
ReadFromQueue()
{
void *tag;
bool ok = false;
while (true)
{
cq_.Next(&tag, &ok);
if (ok)
<--------------- Coming back as false when on a separate thread
{
// do something here
}

}
}

Does anybody have any working sample code I can try?
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.

Vijay Pai

unread,
May 3, 2016, 9:34:32 PM5/3/16
to grpc.io, nid...@gmail.com, micha...@gmail.com

 test/cpp/end2end/thread_stress_test.cc has examples of async clients and servers using different threads to do the sending and the Next; hope that helps.

micha...@gmail.com

unread,
May 4, 2016, 2:10:21 PM5/4/16
to grpc.io, nid...@gmail.com, micha...@gmail.com
Thanks for the pointer.  I was finally able to get this working...

For the next guy that runs into this, I've submitted a PR to hopefully help out:  https://github.com/grpc/grpc/pull/6430

Also if anyone is interested, I've created a PR to update the example async server code to be multi-threaded:  https://github.com/grpc/grpc/pull/6398

Thanks again!
Reply all
Reply to author
Forward
0 new messages