https://kotlinlang.org logo
#coroutines
Title
# coroutines
h

Hexa

03/08/2019, 4:28 PM
Could someone confirm if Kotlin coroutines are stackfull or stackless
e

elizarov

03/09/2019, 9:43 AM
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

pardom

03/09/2019, 2:35 PM
It would be helpful to know your motivation for asking @Hexa.
h

Hexa

03/09/2019, 2:37 PM
I'm building a backend service that I'm trying to optimise and I need to choose between coroutines Vs normal threads
e

elizarov

03/09/2019, 2:39 PM
Trying to optimize for what? What’s is the characteristic you are trying to optimize?
h

Hexa

03/09/2019, 2:39 PM
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

elizarov

03/09/2019, 2:39 PM
Define speed.
h

Hexa

03/09/2019, 2:40 PM
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

elizarov

03/09/2019, 2:41 PM
What kind of API calls are that?
h

Hexa

03/09/2019, 2:42 PM
Aws API calls like calling DynamoDb
S3 , IoT etc
e

elizarov

03/09/2019, 2:43 PM
So, you want to make those call concurrently to reduce responce time, as far as I understand.
h

Hexa

03/09/2019, 2:43 PM
Yep
Problem is some of them needs to be done in order
e

elizarov

03/09/2019, 2:44 PM
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

Hexa

03/09/2019, 2:44 PM
My service sometimes takes more than 29seconds to run hence it timesout
e

elizarov

03/09/2019, 2:44 PM
Even with synchronous API calls you can benefit from coroutines, because your code would look nicer.
h

Hexa

03/09/2019, 2:51 PM
Is it worth sending all my API calls using `async`then even if synchronisation or should I use
launch
?
e

elizarov

03/09/2019, 2:51 PM
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

Hexa

03/09/2019, 2:54 PM
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

elizarov

03/09/2019, 2:58 PM
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

Hexa

03/09/2019, 3:01 PM
That's makes sense. Thanks for clarifying
e

elizarov

03/09/2019, 3:02 PM
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

Hexa

03/09/2019, 6:07 PM
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

elizarov

03/10/2019, 6:13 AM
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.
4 Views