Hi guys, can anyone help me figure out how to call...
# ktor
s
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
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
Sure
e
@cy
s
And on a related note, is there any cleaner way of writing this? I can't seem to
.await()
inside the
call.respond()
g
@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
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
This looks like a classloaders related issue since
SuspendFunctionGun
implements the interface
Do you have hot-reload configured?
s
Yes
And if I disable it, it looks like it does work.
c
Then for sure it is a bug. Could you please report it https://github.com/ktorio/ktor/issues/new
s
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
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
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
What engine did you use before?
s