Add priority hint for CoroutineDispatchers
See original GitHub issueIt will be useful for the tasks which depend on the quality of service. For example, we have N
abstract clients using the common thread pool via CoroutineDispatcher. In rare moments a few clients have tasks with high priority to execution and they should be computed as soon as possible.
Note: If we create multiple pools for each priority, the situation will turn out that if there are no tasks with a certain priority, some pools will be idle while others may be overloaded.
I guess, that the problem can be solved like that:
- Create new
CoroutineContextElement
(for example,CoroutinePriority
); - If
CoroutinePriority
is provided byCoroutineContext
, wrap a dispatched task into an object which can be compared by taken priority and put it into the executor.
The new context element can thus be used:
launch(Dispatchers.Default + CoroutinePriority(42)) {
...
}
The element behaves like a hint. It works only in cases when the dispatcher created from custom thread pool with PriorityQueue and the priority is provided by context, so it produces no overhead in the other cases.
Unfortunately, the most of classes related with Dispatchers are internal in the coroutines package, so it isn’t possible to create this logic outside the coroutines library without pain. The another possible solution is to provide the extended API.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
You don’t really need to maintain all the internals. Look, a dispatcher implementation can be as simple as that:
Hey, Im having the same request and wanted to have on one hand some validation that this is how it should be written and on the other hand to have some online example to copy.
is this the way @elizarov you would do it? The priorityqueue/threadexecutor under the hood is taken from java examples, I just want to be able to also run suspending functions when I call it