lholden / job_scheduler

A simple cron-like job scheduling library for Rust.
Apache License 2.0
199 stars 34 forks source link

How to use async function in run thread? #27

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`
marvinpan8 commented 2 years ago

Solved by tokio 1.17.0