-
Hi! I test the blocking thread. Sample code [dependencies]
tokio = { version = "1.0", features = ["full"] } use std::time::Duration;
use tokio::task::spawn_blocking;
let runtime = match tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.max_blocking_threads(2)
.enable_io()
.enable_time()
.build()
{
Ok(runtime) => runtime,
Err(e) => panic!(e),
};
runtime.block_on(async move {
for n in 1..=10 {
spawn_blocking(move || {
println!("start {}", n);
std::thread::sleep(Duration::from_secs(1));
println!("end {}", n);
});
}
}); Result
Is spawn_bloking only execute on blocking threads? Thank you. |
Beta Was this translation helpful? Give feedback.
Answered by
Kestrer
Feb 18, 2021
Replies: 1 comment 1 reply
-
I think this is because during runtime shutdown Tokio will repurpose worker threads to become blocking threads. If you add a |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
omikuji
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think this is because during runtime shutdown Tokio will repurpose worker threads to become blocking threads. If you add a
std::thread::sleep(Duration::from_secs(10));
before the end ofmain
, only two blocking tasks will be run at once.