Could someone confirm if Kotlin coroutines are sta...
# coroutines
h
Could someone confirm if Kotlin coroutines are stackfull or stackless
e
It depends on your definition of stackfull and stackless 🧌
(to clarify, this distinction is usually artificial and does not have any practical and even theoretical sense).
p
It would be helpful to know your motivation for asking @Hexa.
h
I'm building a backend service that I'm trying to optimise and I need to choose between coroutines Vs normal threads
e
Trying to optimize for what? What’s is the characteristic you are trying to optimize?
h
So I Wanted to cknow how coorutines work
Excuse typo from my fone
Optimise the speed
Because I make too many API calls. About 23 API calls
e
Define speed.
h
And my service takes more than 25seconds to respond
API gateway on Aws has aws hard timeout limit of 29seconds
So I am looking for ways to push some of my API calls in background task using async coroutines
e
What kind of API calls are that?
h
Aws API calls like calling DynamoDb
S3 , IoT etc
e
So, you want to make those call concurrently to reduce responce time, as far as I understand.
h
Yep
Problem is some of them needs to be done in order
e
As long as you are not looking at scale (processing thousands requests at the same time), it does not really mater if you async or not async, coroutines or threads. But coroutine usually help you to structure your code nicer and in a more readable way.
h
My service sometimes takes more than 29seconds to run hence it timesout
e
Even with synchronous API calls you can benefit from coroutines, because your code would look nicer.
h
Is it worth sending all my API calls using `async`then even if synchronisation or should I use
launch
?
e
Use
async
when you need result later, use
launch
if you don’t
like:
Copy code
coroutineScope {
    val a = async { loadA() }
    val b = async { loadB() }
    launch { save(a.await(), b.await()) }
}
async - for result, launch - for side-effect
h
Nice. Thanks
When calling async I thought I need to call await too to get its result? Because it returns Deffered eg. DefferedString so to get the actual I have to call something like a.wait()?
And b.await() from your example
e
Yes. You do a series of
async { ... }
for the things you want to do concurrently, then
await
them later. Don’t do
async { ... }.await() ; async { ... }.await()
as it makes no sense — you don’t get any concurrency (you start the second one only when previous one completes)
h
That's makes sense. Thanks for clarifying
e
No need to await for things you
launch { ... }
for a side-effect. They will be automatically awaited at the end of your
coroutineScope { ... }
You can also establish coroutineScope using
runBlocking { ... }
if you top-level method must be blocking.
But if you use blocking APIs, make sure all your coroutines are launched in background dispatcher —
<http://Dispatchers.IO|Dispatchers.IO>
.
h
Thanks will read. I may come back later if I have more questions but thanks for the detailed info
What if most of my API calls needs to be done in specific sequence? Then calling async won't work as I need to wait for the result of the previous call before proceeding? @elizarov
e
Moreover, you will not get any speedup. If it is slow, it will be slow.
And you don’t need async in this case. You just call them one-by-one.