mvniekerk / tokio-cron-scheduler

Schedule tasks on Tokio using cron-like annotation
Apache License 2.0
530 stars 59 forks source link

How to use async function in run thread? #10

Closed marvinpan8 closed 2 years ago

marvinpan8 commented 2 years ago

How to use async function in run thread? eg:

    let job = Job::new_repeated_async(Duration::from_secs(10), |_uuid, _l| {
        Box::pin(async move {
            let a = schedule::guahao::schedule_91160().await;
            println!("===> {}", a);
        })
    }).unwrap();

but report this error:

error: generator cannot be shared between threads safely
  --> src\main.rs:44:9
   |
44 | /         Box::pin(async move {
45 | |             let a = schedule::guahao::schedule_91160().await;
46 | |             println!("===> {}", a);
47 | |         })
   | |__________^ future created by async block is not `Sync`
   |
   = help: the trait `Sync` is not implemented for `(dyn std::future::Future<Output = Result<http::response::Response<hyper::body::body::Body>, hyper::error::Error>> + std::marker::Send + 'static)`
   = note: required for the cast to the object type `dyn std::future::Future<Output = ()> + Sync + std::marker::Send`
mvniekerk commented 2 years ago

Hi @marvinpan8, thank you for the bug report. On the compiling error - Sync is not implemented for the the return of your .await return. This is unfortunately outside the realm of this library and what I'm seeing it looks like it in the realm of http and/or Hyper's library.

Something similar was reported earlier: https://github.com/mvniekerk/tokio-cron-scheduler/issues/8 The suggested workaround was to send a trigger to an MPSC channel and then awaiting the return of that.

I'm closing this - please feel free to keep on commenting on this bug report, I'll try to assist.