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

trathschlag

02/04/2019, 9:00 AM
Is there any (planned) concept of priorities for coroutines? I imagine an example where I want some specific kind of work – i.e. user interaction handling – to be always executed first, while some cpu intensive work without real time requirements should be handled as soon as all higher priority continuations have been worked on.
u

uli

02/04/2019, 10:29 AM
Hi Till, does your use case allow to put the coroutines on dispatchers that are backed by prioritized threads?
t

trathschlag

02/04/2019, 11:28 AM
Hi Uli! Not really, I want this to be platform independent. So it should work in single threaded environments like the browser.
g

gildor

02/04/2019, 11:47 AM
I believe it’s possible to implement for single threaded dispatchers, priority can be passed as coroutine context. Maybe make sense to create a feature request on kotlinx.coroutines
e

elizarov

02/04/2019, 11:58 AM
It is definitely possible, however I believe the use-case is too domain-specific to warrant its inclusion into the core library. However, you can easily write a prioritized coroutine dispatcher in your own project.
1
t

trathschlag

02/04/2019, 12:02 PM
@gildor For a fixed number of priorities it should also work with multiple workers. Just dispatch round-robin for every priority. Or do I have a misconception here?
g

gildor

02/04/2019, 12:03 PM
Yes, it may work with multiple workers too of course, but use case of priority for single multi threaded dispatcher is less obvious for me Also curious about your use case
t

trathschlag

02/04/2019, 12:04 PM
@elizarov That's no problem for me. I just wanted to make sure that this is not already possible with the core lib
@gildor Actually several use cases: On the backend (JVM) prioritization of more important tasks. The system has always enough work to do, so our CPUs are at 100% all the time. And on the frontend (js) I'm currently experimenting with coroutines and I want some things to be always done first, animation rendering for example
5 Views