Single-threaded actix-web...?

Hi.

So, I am trying to build a web application in Rust. And each time I create a new actix-web project, I have this kind of code:

#[tokio::main]
async fn main() {
  let _ = HttpServer::new(|| {
    App::new()
  })
    .bind("127.0.0.1:5024")
    .unwrap()
    .run()
    .await;
}

However, this simple web application spawns so much thread (even if I try to set the worker count to 1)

How do I make the actix-web to run on the currently running thread instead of spawning new threads?

I'm afraid you can't. HttpServer will always start at least one worker thread per address you bind your server to. You can limit your example to a worker thread and the main thread, but not to just the main thread using HttpServer::workers:

#[tokio::main]
async fn main() {
  let _ = HttpServer::new(|| {
    App::new()
  })
    .bind("127.0.0.1:5024")
    .workers(1)
    .unwrap()
    .run()
    .await;
}
1 Like

Hi @jofas

Thank you for answer. And yeah, I've been exploring actix-web and it looks like there's no way to accomplish what I want.

If you have any alternatives to actix-web (for this specific purpose), I'd be happy to hear one.

Axum is quite similar to Actix-web. Warp is also pretty popular. Both are build on top of hyper and you can run them in a single-threaded tokio runtime.

3 Likes

Thank you, I'll look into each of them

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.