将非 Copy 变量移动到异步闭包中:捕获的变量无法转义“FnMut”闭包主体

Moving non-Copy variable into async closure: captured variable cannot escape `FnMut` closure body

我正在尝试让 clokwerk 每隔 X 秒将异步函数安排到 运行。

The docs 显示此示例:

// 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_pool2config2 可能比借用的值寿命更长,并建议添加 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

有人可以帮助我了解问题所在以及如何解决吗?

注意:我以前见过这个问题,但我很难将答案应用到我的案例中。

我也是初学者,所以也许他们确实适用,但我看不出有什么联系:)

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

您的原始代码:

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

相当于:

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

所以你创建了一个闭包,它是 FnMut 类型(returns 是实现 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);
            }
        });