spawn_with_priority

Function spawn_with_priority 

Source
pub fn spawn_with_priority<Fut>(
    future: Fut,
    priority: Priority,
) -> Task<Fut::Output>
where Fut: Future + Send + 'static, Fut::Output: Send,
Expand description

Creates a new task with the specified execution priority.

This allows fine-grained control over task scheduling, enabling background tasks to yield to higher-priority operations.

§Arguments

  • future - The future to execute asynchronously
  • priority - The scheduling priority for this task

§Returns

A Task handle that can be awaited to retrieve the result

§Examples

use native_executor::{spawn_with_priority, Priority};

// High-priority task for time-sensitive operations
let urgent = spawn_with_priority(async {
    // Your time-sensitive work here
    42
}, Priority::Default);

// Background task that won't interfere with UI responsiveness
let cleanup = spawn_with_priority(async {
    // Your background work here
    "done"
}, Priority::Background);
Examples found in repository?
examples/priority.rs (lines 14-21)
4fn main() {
5    // Spawn a default priority task
6    spawn(async {
7        println!("Default priority task started");
8        Timer::after_secs(1).await;
9        println!("Default priority task completed");
10    })
11    .detach();
12
13    // Spawn a background priority task
14    spawn_with_priority(
15        async {
16            println!("Background priority task started");
17            Timer::after_secs(1).await;
18            println!("Background priority task completed");
19        },
20        Priority::Background,
21    )
22    .detach();
23
24    // Keep the main thread alive
25    std::thread::sleep(Duration::from_secs(2));
26}