[英]GRPC future vs blocking stub in client
I'm writing a client application in Kotlin where I need to call a GRPC service that has both a future based and a blocking stub.我正在 Kotlin 中编写一个客户端应用程序,我需要调用一个 GRPC 服务,该服务同时具有基于未来和阻塞的存根。
In case I choose to go with the future option, I will need at some point to do something like a CompletableFuture.get()
call, which means I will be blocking anyway.如果我选择带有 future 选项的 go,我将需要在某个时候执行类似
CompletableFuture.get()
调用的操作,这意味着我无论如何都会阻塞。
So, what's the difference between one and the other for this case?那么,在这种情况下,两者之间有什么区别? Or is it the same?
或者是一样的?
I will need at some point to do something like a CompletableFuture.get() call, which means I will be blocking anyway
在某些时候我需要做一些类似 CompletableFuture.get() 调用的事情,这意味着我无论如何都会阻塞
Not necessarily.不必要。 With a
ListenableFuture
, CompletableFuture
, or other completion-hookable mechanisms, you don't have to block at all.使用
ListenableFuture
、 CompletableFuture
或其他完成挂钩机制,您根本不必阻塞。 You could register an action to do on completion, or you could wrap them in suspend
calls to work with coroutines etc.您可以注册一个在完成时执行的操作,或者您可以将它们包装在
suspend
调用中以使用协程等。
Also, even if you did call get()
and block, it still allows you to block later than the start of the call, and do other things between the initial call and the get()
, which would be concurrent with it:此外,即使您确实调用了
get()
并阻塞,它仍然允许您在调用开始之后阻塞,并在初始调用和get()
之间做其他事情,这将与它并发:
val future = callSomethingWithFuture()
doStuffConcurrently()
val result = future.get()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.