Don't know whether it is proper to ask here, but i feel pretty puzzled about the tokio runtime.
I made a simple server with blocking and non-block handler as below,
when visiting blocking handler from the browser or postman, it would block all of the incoming requests no matter whether those requests are from browser, curl, or anywhere, until the calculation is done then start handling other requests.
when visiting blocking handler from curl, python & requests, or test with rewrk, the whole router works fine, it just handle the following request in another thread, and when the calculation is done, it returns from blocking one.
What makes me most puzzled is visiting from different agents got different results.
Hello @alice ,
Sorry for mentioning you so absurdly, I have read your article Async: What is blocking? – Alice Ryhl and thanks for your great work.
I am a bit confused about When writing async Rust, the phrase “blocking the thread” means “preventing the runtime from swapping the current task”.
Does that mean it just blocks the whole runtime when one of the tasks being tokio::spawn
is blocked?
use tokio;
use axum::{response::Html, routing::get, Router};
use std::net::SocketAddr;
#[tokio::main(flavor = "multi_thread", worker_threads = 16)]
async fn main() {
let app = Router::new()
.route("/blocking/", get(get_block))
.route("/nonblock/", get(non_block));
let addr = SocketAddr::from(([0, 0, 0, 0], 2666));
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
async fn get_block() -> Html<&'static str> {
dbg!(std::thread::current().id());
let mut i = 1;
for _ in (0..=599999999){
i+=1;
}
Html("<h1>blocking!</h1>")
}
async fn non_block() -> Html<&'static str> {
dbg!(std::thread::current().id());
Html("<h1>Non block!</h1>")
}