A yield point passes control back to the asynchronous task scheduler, allowing the runtime to switch tasks instead of waiting on one operation for too long. section 8.3
Use yield points to keep async execution responsive:
- Waiting on or joining a future creates a yield point by passing control through the future chain to the runtime. section 8.3
- Treat I/O as a likely blocking operation, and recognize that sufficiently long CPU-bound work can also block task switching. section 8.3
- Judge whether work is too slow contextually: a small file read can take orders of magnitude longer than an ordinary function call. section 8.3
fn main() {
use std::time;
let duration = time::Duration::from_secs(1);
tokio::runtime::Builder::new_current_thread()
.enable_time() #1
.build()
.unwrap()
.block_on(async { #2
tokio::time::sleep(duration).await;
println!("Hello, world!");
});
}
Compare synchronous sleeping with Tokio async sleeping: awaiting the timer gives the runtime a scheduling opportunity. section 8.3
