asynchronous - 直接使用Future或包装在异步移动 block 之间的区别

标签 asynchronous rust send tokio

我想知道为什么将代码从直接在Future中使用tokio::spawn更改为使用async move块可以使代码编译。
直接使用:

struct ClientMsg {
    ...
    resp: oneshot::Sender<Bytes>,
}

async fn client_thread(
    mut rx: Receiver<ClientMsg>,
    client: Client,
) -> Result<(), Box<dyn Error>> {
    while let Some(msg) = rx.recv().await {
        ...
        let response = client.get(url).send().await?.bytes().await?;
        msg.resp.send(response).unwrap();
    }
    Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    ...
    let (tx, rx) = mpsc::channel(5);
    tokio::spawn(client_thread(rx, client)); // <--- Difference is here

    Ok(())
}
异步块:
struct ClientMsg {
    ...
    resp: oneshot::Sender<Bytes>,
}

async fn client_thread(
    mut rx: Receiver<ClientMsg>,
    client: Client,
) -> Result<(), Box<dyn Error>> {
    while let Some(msg) = rx.recv().await {
        ...
        let response = client.get(url).send().await?.bytes().await?;
        msg.resp.send(response).unwrap();
    }
    Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    ...
    let (tx, rx) = mpsc::channel(5);
    tokio::spawn(async move { client_thread(rx, client) }); // <-- Difference is here

    Ok(())
}
请注意以下use:
use bytes::Bytes;
use reqwest::Client;

use tokio::sync::{
    mpsc::{self, Receiver},
    oneshot,
};
use url::Url;
直接使用代码因以下原因而失败:
error[E0277]: `(dyn StdError + 'static)` cannot be sent between threads safely
   --> src/main.rs:44:5
    |
44  |     tokio::spawn(client_thread(rx, client, base_url));
    |     ^^^^^^^^^^^^ `(dyn StdError + 'static)` cannot be sent between threads safely
    | 
   ::: /home/jeanluc/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/task/spawn.rs:130:20
    |
130 |         T::Output: Send + 'static,
    |                    ---- required by this bound in `tokio::spawn`
    |
    = help: the trait `Send` is not implemented for `(dyn StdError + 'static)`
    = note: required because of the requirements on the impl of `Send` for `Unique<(dyn StdError + 'static)>`
    = note: required because it appears within the type `Box<(dyn StdError + 'static)>`
    = note: required because it appears within the type `std::result::Result<(), Box<(dyn StdError + 'static)>>`
client_thread的返回类型与main函数完全相同,但是它与Tokio一起运行没有任何问题。此外,error type from reqwest 实现Send

最佳答案

完成后,将函数调用包装在async {}块中是行不通的。运行时,它将调用该函数,该函数将创建 future ,并仅返回它。永远不会对其进行轮询,因此永远不会取得进展。直接使用它或与.await一起使用可确保对其进行轮询。

use futures::executor::block_on; // 0.3.12

async fn f(s: &str) {
    println!("ran {}", s);
}

fn main() {
    block_on(f("first"));
    block_on(async { f("second") }); // this doesn't print
    block_on(async { f("third").await });
}
ran first
ran third
由于 future 不会持续存在,因此它不会最终影响async {}块的特征,因此可以是Send.await会再次遇到相同的问题。

在您的情况下,您需要做的就是确保将来实现Send,以便可以与tokio::spawn一起运行。解决方法是确定所返回的Error特性实现了Send:
async fn client_thread(
    mut rx: Receiver<ClientMsg>,
    client: Client,
) -> Result<(), Box<dyn Error + Send>> {
    // ...                    ^^^^^^
}

关于asynchronous - 直接使用Future或包装在异步移动 block 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66295966/

相关文章:

rust - 为字段创建具有通用特征的结构。预期的 struct<trait> 找到了 struct<type that implements said trait>

rust - 变量具有不同的生命周期,而它们本应具有相同的生命周期

iphone - 异步图片加载闪烁?

javascript - 是否需要setTimeout?

java - 一些 java 处理程序仅在结果可用时获取 url.openStream()

rust - 如何将std::sync::mpsc::Sender <T>传递给warp中的处理程序?

javascript - 异步函数声明在 Internet Explorer 中需要 ';'

c# - Socket.Send 和 Stream.Write 有什么区别? (与 tcp ip 连接相关)

python "send"方法不会更改 "next"的值?

php - 通过 PHP 在电子邮件中发送 HTML