Skip to content

how to use tokio::spawn in rust client? #598

Open
@sternezsl

Description

I would like to concurrently send requests to fbthrift server, so I created a client with the following code:

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let stream = tokio::net::TcpStream::connect("127.0.0.1:6060").await?;
    let transport = TcpTransport::new(stream);
    let conn = <dyn fbthrift_demo_if::TestService>::new(CompactProtocol, transport);
    let client1 = conn.clone();

    let mut tasks = vec![];
    tasks.push(tokio::spawn(async move {
        let result = client1.method1(10).await;
        println!("result: {}", result.unwrap());
    }));

    let client2 = conn.clone();
    tasks.push(tokio::spawn(async move {
        let result = client2.method1(11).await;
        println!("result: {}", result.unwrap());
    }));
    for task in tasks {
        task.await?;
    }
    Ok(())
}

where the TestService::method1 is very simple, it just adds one to the received number

service TestService {
  i32 method1(1: i32 req);
}

the client subspended after receive the first request's result:

> cargo run -r
result: 11

but if I tweak the code with use futures::future::try_join_all

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let stream = tokio::net::TcpStream::connect("127.0.0.1:6060").await?;
    let transport = TcpTransport::new(stream);
    let conn = <dyn fbthrift_demo_if::TestService>::new(CompactProtocol, transport);
    let response = try_join_all(vec![conn.method1(10), conn.method1(11)]).await;
    println!("{response:?}");
    Ok(())
}

it works and I got the expected result:

> cargo run -r
Ok([11, 12])

Myy question is what is wrong with the tokio::spawn version?

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions