3
votes

Is there a way to specify a single-threaded runtime using the attribute #[tokio::main] in tokio 0.2? The doc does not seem to have examples for that.

EDIT: I wanted to find a way to set up tokio runtime so that rustc is aware of tokio:spawn() will not be a new thread.

1

1 Answers

6
votes

The documentation for tokio::main shows what options it has:

core_threads=n - Sets core threads to n (requires rt-threaded feature). max_threads=n - Sets max threads to n (requires rt-core or rt-threaded feature).

Thus:

#[tokio::main(core_threads = 1, max_threads = 1)]
async fn main() {
    println!("Hello world");
}

If this does not work for whatever your case is, you will have to directly create a runtime as shown in How do I synchronously return a value calculated in an asynchronous Future in stable Rust?.