If I wanted to start some block as a long running ...
# coroutines
a
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
You should use
coroutineScope { ... }
a
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
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
@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
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
Ah of course, thanks @lukaswelte