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

Albert

10/05/2018, 8:14 AM
If I wanted to start some block as a long running process with multiple `async`/`launch`, should I use
withContext
?
Copy code
withContext(GlobalScope.coroutineContext) {
  async {
  }
  launch {
  }
}
e

elizarov

10/05/2018, 8:42 AM
You should use
coroutineScope { ... }
a

Albert

10/05/2018, 9:02 AM
Lets say I would use ktor like so:
Copy code
routing {
  get("/foo") {
    coroutineScope { ... }
  }
}
Wouldn't that mean that if GET is done the coroutineScope would be cancelled (of course it depends on how ktor will implement this). So in this case even after the GET request is done, I don't the coroutine top be cancelled. Or am I understanding coroutineScope wrong?
l

lukaswelte

10/05/2018, 9:36 AM
What do you mean by GET is done? For ktor you don’t even need an extra scope since a ktor routing already provides a scope to execute the coroutines in
Are you asking how to start a long running process which is not bound to the request (when request finishes it still runs)?
a

Albert

10/05/2018, 9:43 AM
@lukaswelte Yeah that is what I am asking. I know ktor doesn't have that. Probably wrong example. I know I could use
GobalScope.async
for this. But in my example I wanted to start multiple `async`/`launch` without have to use
GlobalScope.
prefix all the time.
l

lukaswelte

10/05/2018, 9:46 AM
You could use
with(GlobalScope) {}
I’m not sure there is a coroutine specific answer to launching multiple things on the global scope
👍 1
a

Albert

10/05/2018, 9:47 AM
Ah of course, thanks @lukaswelte