Skip to content

Multithreading support on Server #1575

Answered by som3on3
lmtr0 asked this question in Q&A
Discussion options

You must be logged in to vote
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let cpus = 5;
    println!("Running with {} threads", cpus);

    // Esentially the same as tokio::main, but with number of threads set
    tokio::runtime::Builder::new_multi_thread()
        .worker_threads(cpus)
        .enable_all()
        .build()
        .unwrap()
        .block_on(serve())
}

async fn serve() -> Result<(), Box<dyn std::error::Error>> {
    let addr = "0.0.0.0:50051".parse().unwrap();
    let greeter = MyGreeter::default();

    println!("GreeterServer listening on {}", addr);

    Server::builder()
        .add_service(GreeterServer::new(greeter))
        .serve(addr)
        .await?;

    Ok(())
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@lmtr0
Comment options

Answer selected by lmtr0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants