I'm working on a Ktor application where I need to ...
# ktor
f
I'm working on a Ktor application where I need to launch a coroutine within a route handler that should continue running even after the request has been completed and the response has been sent. Considering that using
GlobalScope
should be avoided due to its lack of structured concurrency, what is the best practice for achieving this? I was thinking of creating a scope in the route, but is there a better way to implement this?
Copy code
fun Route.exampleRoute() {
    get("/example") {
        launch {
            // This coroutine should continue running even after the response is sent
            delay(1000)
            println("Coroutine finished")
        }
        call.respondText("Request handled")
    }
}
s
Can you create a coroutine outside of the route definition completely, which will live as long as the entire application lives, and gets cancelled when the entire application receives the signal to shut down itself?