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

Sam

05/04/2019, 2:53 PM
Is there any support for priority by coroutines?
e

elizarov

05/04/2019, 3:03 PM
What are you trying to achive?
s

Sam

05/04/2019, 3:04 PM
Its a list view in an app. Each item has an associated async task and scrolling the list creates a task to fetch new items. I'm trying to give that fetch task more priority over the other pending tasks
btw all tasks perform IO over network
e

elizarov

05/04/2019, 3:06 PM
I fear that is not something coroutines can help you with. I assume that fetch task goes somewhere over the network, so you should look to your network IO stack to see if it supports tweaking priorities of outstanding requests.
Coroutines themselves would take no cpu time — they just fire off a network request. So prioritizing coroutines themselves is of no help.
It is network requests you need to prioritize
s

Sam

05/04/2019, 3:07 PM
I was hoping if Dispatchers supported some concept of priority
e

elizarov

05/04/2019, 3:08 PM
You can write a prioritized dispatcher. But that would not help in solving your problem.
Dispatchers manage CPU consumption. They don’t manage network consumption. And in your problem the limited resource you want to prioritize is a network, not cpu.
s

Sam

05/04/2019, 3:09 PM
Okay
r

r4zzz4k

05/04/2019, 4:46 PM
I may be totally wrong as I still didn't use
select
once, but my idea is as follows: one channel for posting `Unit`s or objects with some data when fetching is needed, one channel for posting items to run actions upon and one coroutine selecting from those channels and running corresponding IO operations for both of these.
select
is biased to the upper statements. So your coroutine would usually select from second channel and run actions on items, but as soon as there is a message in first channel, it would be taken instead of second one and fetching gets a prioritized processing.
2 Views