Search code examples
rustrust-tokiorust-axum

Is it possible to run Axum on a single thread?


I know that Axum is built on top of Tokio and Tokio has a multi-threaded scheduler and current-threaded scheduler.

Is it possible to set the runtime to make it serve the requests in single thread?


Solution

  • Since axum is just spawned in a tokio runtime with the #[tokio::main] macro or a system you setup. The futures are handled the way you configured the runtime.

    Just do this if you're using the macro:

    #[tokio::main(flavor = "current_thread")]
    async fn main() {
        // Axum code here...
    }
    

    Here's the docs for more information for the macro: https://docs.rs/tokio/0.3.3/tokio/attr.main.html#current-thread-runtime

    And here's the non sugared way to setup a tokio runtime: https://docs.rs/tokio/latest/tokio/runtime/struct.Builder.html#method.new_current_thread