https://kotlinlang.org logo
#ktor
Title
# ktor
s

Sander Ploegsma

02/25/2019, 7:59 AM
Hi guys, can anyone help me figure out how to call two
suspend
functions in parallel inside a route? This compiles but crashes at runtime with `io.ktor.util.pipeline.SuspendFunctionGun cannot be cast to kotlinx.coroutines.CoroutineScope`:
Copy code
fun Routing.test() {
    get("/") {
        val firstResultDeferred = async { firstCall() }
        val secondResultDeferred = async { secondCall() }
        
        val firstResult = firstResultDeferred.await()
        val secondResult = secondResultDeferred.await()

        call.respond(listOf(firstResult, secondResult))
    }
}
g

gildor

02/25/2019, 8:03 AM
Could you show full stacktrace, looks like some ktor bug (make sense to create an issue)
because your code in general is completely fine
See, this works with coroutines: https://pl.kotl.in/BymrGQ-8N
s

Sander Ploegsma

02/25/2019, 8:09 AM
Sure
e

e5l

02/25/2019, 8:10 AM
@cy
s

Sander Ploegsma

02/25/2019, 8:22 AM
And on a related note, is there any cleaner way of writing this? I can't seem to
.await()
inside the
call.respond()
g

gildor

02/25/2019, 8:25 AM
@Sander Ploegsma what do you mean “inside the `call.respond()`“? You can, because get block is suspendable lambda, I showed above example that it works
s

Sander Ploegsma

02/25/2019, 8:29 AM
I would like to do this:
Copy code
get("/test") {
        val a = async { a() }
        val b = async { b() }

        call.respond(listOf(a.await(), b.await()))
    }
EDIT: Oh wait, never mind. In my example it works, but my production code uses
call.respondHtmlTemplate
with a block function parameter that is not suspendable
c

cy

02/25/2019, 8:30 AM
This looks like a classloaders related issue since
SuspendFunctionGun
implements the interface
Do you have hot-reload configured?
s

Sander Ploegsma

02/25/2019, 8:32 AM
Yes
And if I disable it, it looks like it does work.
c

cy

02/25/2019, 8:42 AM
Then for sure it is a bug. Could you please report it https://github.com/ktorio/ktor/issues/new
s

Sander Ploegsma

02/25/2019, 8:59 AM
I set up a small MVP to recreate the bug but it looks like that works: https://github.com/sanderploegsma/ktor-route-async-await So, it's probably an issue with our current setup, let me look into it
g

gildor

02/25/2019, 9:11 AM
but my production code uses
call.respondHtmlTemplate
with a block function parameter that is not suspendable
Yes, it make sense. You can create a feature request to make respondHtmlTemplate suspendable. For now I think the only way is suspend before this block. receive data and than use it in respondHtmlTemplate block
s

Sander Ploegsma

02/25/2019, 9:47 AM
Thanks @gildor, I submitted a feature request for it: https://github.com/ktorio/ktor/issues/976
@cy It looks like an issue with AppEngine, which is what my application runs on. If I run it using
io.ktor.server.jetty.EngineMain
everything works as expected, also with hot reloading enabled
c

cy

02/25/2019, 9:55 AM
What engine did you use before?
s

Sander Ploegsma

02/25/2019, 9:57 AM