将非复制变量移动到异步闭包中:捕获的变量无法转义`FnMut`闭包主体

我试图让clokwerk安排一个异步函数每 X 秒运行一次。

文档显示了这个例子:

// Create a new scheduler
let mut scheduler = AsyncScheduler::new();
// Add some tasks to it
scheduler
    .every(10.minutes())
        .plus(30.seconds())
    .run(|| async { println!("Simplest is just using an async block"); });
// Spawn a task to run it forever
tokio::spawn(async move {
  loop {
    scheduler.run_pending().await;
    tokio::time::sleep(Duration::from_millis(100)).await;
  }
});

我的初步尝试:

    let config2 = // define a Config struct, Config
    let pg_pool2 = // get a sqlx connection pool, Pool<Postgres>

    //I assume I need shared references so I use Arc
    let pg_pool2 = Arc::new(pg_pool2);
    let config2 = Arc::new(config2);

    let mut scheduler = AsyncScheduler::new();

    scheduler.every(5.seconds()).run(|| async {
        println!("working!");
        pull_from_main(pg_pool2.clone(), config2.clone()).await;
    });

    tokio::spawn(async move {
        loop {
            scheduler.run_pending().await;
            tokio::time::sleep(Duration::from_millis(100)).await;
        }
    });

编译器抱怨pg_pool2并且config2可能比借用的值更长寿,并建议添加move. 公平的。让我们试试看。

我的第二次尝试:

    //rest the same
    scheduler.every(5.seconds()).run(move || async {
    //rest the same

这次我得到了一个我无法自己破译的错误:

error: captured variable cannot escape `FnMut` closure body
  --> src/main.rs:80:46
   |
75 |       let pg_pool2 = Arc::new(pg_pool2);
   |           -------- variable defined here
...
80 |       scheduler.every(5.seconds()).run(move || async {
   |  ____________________________________________-_^
   | |                                            |
   | |                                            inferred to be a `FnMut` closure
81 | |         println!("working!");
82 | |         pull_from_main(pg_pool2.clone(), config2.clone()).await;
   | |                        -------- variable captured here
83 | |     });
   | |_____^ returns an `async` block that contains a reference to a captured variable, which then escapes the closure body
   |
   = note: `FnMut` closures only have access to their captured variables while they are executing...
   = note: ...therefore, they cannot allow references to captured variables to escape

有人可以帮助我了解出了什么问题以及如何解决吗?

注意:我之前看过这个问题,但我正在努力将答案应用于我的案例。

  • 这里涉及一个可变变量,我没有。
  • 这里的解决方案是将变量作为 fn 参数包含在内,我不能这样做,因为闭包不需要参数。当我不这样做时,它似乎也从闭包返回了一个值,这会导致问题。

我也是初学者,所以也许他们确实申请了,但我看不到联系:)

回答

为了理解发生了什么,我将重新格式化代码以使其更加清晰和明确:

您的原始代码:

  scheduler
    .every(5.seconds())
    .run(move || async {
        do_something(arc.clone());
    });

相当于:

  scheduler
    .every(5.seconds())
    .run(move || {
       return async {
          do_something(arc.clone());
       }
    });

所以你创建了一个闭包,它是一个类型FnMut(并且它返回一个实现 的类型Future)。这意味着您的闭包可以被多次调用,并且每次调用都应该产生一个新的未来。但是return async{} Arc的闭包移出,这意味着它只能被调用一次。想象一下,你的钱包里有一张 10 美元的钞票。如果你把它拿出来花掉,那你就不能再拿出来花掉了,因为它根本就不存在了。

那么我们该如何解决呢?实际上很容易 -Arc在将它移动到async块之前你必须克隆你的。因此,您将只移动克隆:

    let arc = Arc::new(whatever);   
    scheduler
        .every(5.seconds())
        .run(move || {
             // Clone the arc and move the clone!!! 
             // The original arc will remain in the closure, 
             // so it can be called multiple times.
            let x = arc.clone(); 
            async move { 
                do_something(x);
            }
        });


以上是将非复制变量移动到异步闭包中:捕获的变量无法转义`FnMut`闭包主体的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>