-
Hi, I was researching how to set up the workers logic, like in gunicorn/uvicorn/etc. But I can't find any ways to serve requests concurrently. Here's a simple example of the app that simulates some async operation: use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use axum::{routing::get, Router};
use tokio::time;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
const ADDR: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8080);
#[tokio::main]
async fn main() {
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
"my_app=debug,tower_http=debug,axum::rejection=trace".into()
}),
)
.with(tracing_subscriber::fmt::layer())
.init();
// build our application with a single route
let app = Router::new().route(
"/",
get(|| async move {
tracing::debug!("got request");
let _ = time::sleep(time::Duration::from_secs(5)).await;
tracing::debug!("awaiting done");
"lol"
}),
);
// run it
axum::Server::bind(&ADDR)
.serve(app.into_make_service())
.await
.unwrap();
} After sending 2 requests at the same time it handles them one by one but not at the same time, so it takes 5 * 2 = 10 seconds on it: 2023-10-20T14:53:49.787721Z DEBUG my_app: got request
2023-10-20T14:53:54.792423Z DEBUG my_app: awaiting done
2023-10-20T14:53:54.793303Z DEBUG my_app: got request
2023-10-20T14:53:59.794561Z DEBUG my_app: awaiting done So I can't get the idea on how to do it with using |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
axum doesn’t handle that. It just does request -> response. Tokio handles the async runtime. You can configure it via the |
Beta Was this translation helpful? Give feedback.
-
The code looks perfectly fine. How are you testing it? |
Beta Was this translation helpful? Give feedback.
Oh, sheesh, tried to use
curl
instead of dumb browser and I got it working this time:I guess browser uses some kinda synchronization between multiple tabs of the single domain name, maybe that's why it works not as expected. Anyway thanks for implying to use other ways to test it.
P.s.: just sick of reading all related questions where the author uses
std::thread::sleep
so I haven't found any answered ones with proper sleeping strategy