Is there any (planned) concept of priorities for c...
# coroutines
t
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
Hi Till, does your use case allow to put the coroutines on dispatchers that are backed by prioritized threads?
t
Hi Uli! Not really, I want this to be platform independent. So it should work in single threaded environments like the browser.
g
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
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
@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
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
@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