Is gRPC streaming thread safe?

3,625 views
Skip to first unread message

song...@hotmail.com

unread,
Dec 21, 2016, 12:59:29 PM12/21/16
to grpc.io
The gRPC streaming can be bi-directional and async. Is it thread safe? Suppose that I have an RPC method with bi-directional streaming. Can I use one thread to read the stream, and another thread to write the stream simultaneously? Look forward to your help. Thanks in advance!

Nathaniel Manista

unread,
Dec 21, 2016, 1:48:31 PM12/21/16
to song...@hotmail.com, grpc.io
On Wed, Dec 21, 2016 at 9:59 AM, <song...@hotmail.com> wrote:
The gRPC streaming can be bi-directional and async. Is it thread safe?

That depends on what the threads are doing - reads must be synchronized with respect to one another and writes must be synchronized with respect to one another, so an application cannot have multiple threads writing at the same time.

Suppose that I have an RPC method with bi-directional streaming. Can I use one thread to read the stream, and another thread to write the stream simultaneously?

Yes, this is an intended and fully supported use case.

Just out of curiosity: in what programming language are you using gRPC?
-N

song...@hotmail.com

unread,
Dec 21, 2016, 2:21:04 PM12/21/16
to grpc.io, song...@hotmail.com
Thanks for the reply.

I only have one read thread and one write thread. Do I need to sync them to use the stream?

BTW, I am using c/c++. I am doing a small experiment, and seeing a crash at client side. Checked the code again, I guess likely because the stream I am using is ClientReaderWriter. Probably I should use ClientReader stream for read thread, and ClientWriter stream for write thread, is it right?

Vijay Pai

unread,
Jan 24, 2017, 8:01:21 PM1/24/17
to grpc.io, song...@hotmail.com
Hello,
Sorry for the delayed response.


On Wednesday, December 21, 2016 at 11:21:04 AM UTC-8, song...@hotmail.com wrote:
Thanks for the reply.

I only have one read thread and one write thread. Do I need to sync them to use the stream?

 The read thread and write thread do not need to be synced with each other.
 

BTW, I am using c/c++. I am doing a small experiment, and seeing a crash at client side. Checked the code again, I guess likely because the stream I am using is ClientReaderWriter. Probably I should use ClientReader stream for read thread, and ClientWriter stream for write thread, is it right?

Since you're using a bidi async RPC, then you should be using ClientAsyncReaderWriter. Each of these objects is for an RPC.

- Vijay
 

song...@hotmail.com

unread,
Jan 24, 2017, 8:17:50 PM1/24/17
to grpc.io, song...@hotmail.com
Thank you Vijay!

I already got my bidi client/server worked with sync APIs. However, I really need to use ASYNC APIs with one thread that does multiplexing. I am kind of stuck on that now. Could you please take a look at another post of mine at https://groups.google.com/forum/#!topic/grpc-io/DIeehyvJIs0. Thank you very much!

Vijay Pai於 2017年1月24日星期二 UTC-8下午5時01分21秒寫道:
Reply all
Reply to author
Forward
0 new messages