Is Ktor going to support Coroutines 0.26.0 soon?
# ktor
d
Is Ktor going to support Coroutines 0.26.0 soon?
👌 1
e
isn’t it supported?
d
With the CoroutineScope changes, I doubt it would work now...
e
It will work. All the changes are binary backwards compatible
d
@elizarov, You mean since Ktor's included as a dependency, but if one tries to
launch
something in the app's code, you still need a
CoroutineScope
... I don't know what Ktor will do for that anyways, what will be the Scope, a single request, the application?
e
The scope is the scope. Depends on what you are doing.
You need to provide it depending on what scope you are planning to
launch
your coroutine in. If you plan to launch a coroutine in the scope of request, use the scope of request. If you plan to launch coroutine in the scope of your whole application, use
GlobalScope
.
If you plan to launch coroutine in the scope of some particular block of code, then delimit this block of code with
coroutineScope { ... }
.
d
Thanks for the answer @elizarov, I just had a few more little questions: Isn't a bit heavy to use
coroutineScope { }
for each request, if there is a heavy load of requests? I'd understand for an Android Activity, but requests are things that come and go very quickly. Also, I wonder how it would deal with a request timeout, how would it get cancelled (the scope, I mean)? Also, the fact that Ktor is a DSL and not class based, makes it that nothing can just implement the regular
CoroutineScope
, so the right practice is to use
coroutineScope { }
?
e
Sorry, but I’ve lost the context of your question. What kind of application you are talking about? If you are writing server-side ktor app you usually don’t have to launch any coroutines yourself. You just write your code in ktor DSL and that’s it, you don’t even have to care what
CoroutineScope
is. You only need to if you want to do some kind of parallel decomposition — make several operation concurrently. In this case, you delimit your block with
coroutineScope { ... }
d
On the server side, I need to do some db and jsonrpc requests (with some things coming from a cache), await each, and then compose the response. Sometimes, I even need to do extra processing after having returned the response. So I try to do all that in parallel
But there are times, when one of the datasource requests gets stuck, so I need to be able to have a timeout that cancels all the other things.. @elizarov
For that request, at least
e
Then you can do
withTimeout { ... }
. It also creates a scope of all of the coroutines you launch (in parallel) inside of the block — they will all be cancelled on timeout
d
You mean that the scope created by
coroutineScope {}
will be cancelled if one of those operations in a
withTimeout
times out? And I suppose if I would surround all the operations with that
withTimeout
, it should also work. Thanks! I wonder if it would be enough of a common case to warrent a
coroutineScopeWithTimeout
...