Hi folks - I’m sorry for a newbie question, but my...
# coroutines
r
Hi folks - I’m sorry for a newbie question, but my research didn’t conclude in clear understanding of
runBlocking
usages and I’d love to get some pointer. My goal is to start a blocking process with AWS Lambda, which would kick off a bunch of heavy tasks concurrently, and then returns the aggregate of the tasks when they are all completed (or when any canceled). Because the Lamdba interface itself is not a suspending function (i.e.
RequestHandler<I, O>
interface needs function with signature of
public O handleRequest(I input, Context context)
), I will need a coroutine builder inside
handleRequest
method to await for the tasks. I have read in a few places how
runBlocking
is only meant to be used for unit testing mainly, and should not be used in production. What I want to achieve seems to require
runBlocking
for good reason, but I am unsure if I’m missing something obvious. Given serverless function requiring a blocking call, is
runBlocking
a legitimate approach in this scenario? Or is there something that I’m missing? Thanks in advance for your support!
That makes perfect sense, thanks Andrey! The part I got confused was how
runBlocking
is a bridge between blocking and async, but should not be abused - and what that “abuse” really meant. It would be really nice if the Lambda can fully understand the promise-like abstraction, or even just provide suspending function interface for Kotlin. But in its own implementation, I assume they would likely need to do some blocking like
runBlocking
. It makes sense how
runBlocking
in this scenario is the only way to achieve my goal.
g
It would be really nice if the Lambda can fully understand the promise-like abstraction, or even just provide suspending function interface for Kotlin
Be careful what you wish for. GRPC does this by using callbacks everywhere and it confuses me on a regular basis. Its not too hard to write an adapter (assuming a simple thread-pooling strategy works for you), but then you end up using that adapter liberally.
r
I’m not sure if I fully follow your point - writing an adapter in this case corresponds to using
runBlocking
to wrap the suspending functions, doesn’t it? And that’s how you may end up using it ubiquitously?