Does async have a notion of priority?

For each async task, I want to attach to it overall_priority = (priority: u64, millis_idle_for: u64), and whenever there is more than one task available, have it pick the one with highest overall_priority.

Context: Rust / wasm32. I have a bunch of async tasks on a thread, and seeing if there is a way to prioritize it (moving it off to another web worker would be quite a hassle due to shared state).

EDIT: the above construction is: if a single task has strict higher priority, pick it; of more than one task has highest priority, round robin (based on millis_idle_for).

There’s nothing like that built-in to Rust’s async machinery, but it would fairly easy for an executor to provide this. Making scheduling decisions is the executor's raison d’être, and it has full freedom to do whatever it likes in this area.

That said, I have no idea if any of the popular executors provide such a facility.

1 Like

You might be interested in reading about tokio's cooperative task budgeting. It's kind of the opposite of a priority system, in that long running tasks can get forced to yield to the runtime when they call into a tokio API.

1 Like