Asynchronous-API tutorial
Asynchronous-API tutorial
This tutorial shows you how to write a simple server and client in C++ using gRPC’s asynchronous/non-blocking APIs. It assumes you are already familiar with writing simple synchronous gRPC code, as described in Basics tutorial. The example used in this tutorial follows from the basic Greeter example used in the quick start. You’ll find it along with installation instructions in grpc/examples/cpp/helloworld.
Overview
gRPC uses the CompletionQueue API for asynchronous operations. The basic work flow is as follows:
- bind a
CompletionQueueto an RPC call - do something like a read or write, present with a unique
void*tag - call
CompletionQueue::Nextto wait for operations to complete. If a tag appears, it indicates that the corresponding operation is complete.
Async client
To use an asynchronous client to call a remote method, you first create a channel and stub, just as you do in a synchronous client. Once you have your stub, you do the following to make an asynchronous call:
Initiate the RPC and create a handle for it. Bind the RPC to a
CompletionQueue.CompletionQueue cq; std::unique_ptr<ClientAsyncResponseReader<HelloReply> > rpc( stub_->AsyncSayHello(&context, request, &cq));