简体   繁体   English

A Rust function 返回一个future,after.await(),可能会引发恐慌。? 我怎样才能避免恐慌! 停止程序?

[英]A Rust function return a future, after .await(), it may be throw a panic!. How can I avoid the panic! to stop the program?

I'm making a function call_data() , it will return a future.我正在制作 function call_data() ,它将返回一个未来。 In the main function, I use tokio task to call call_data() forever each 60 seconds.在主 function 中,我使用 tokio 任务每 60 秒永久调用call_data() Some time, the call_data() .await is a Error, so there is a panic.有时, call_data() .await 是一个错误,所以会出现恐慌。 and stop the program.并停止程序。 I try let a = call_data("name", "table").await;我尝试let a = call_data("name", "table").await; , then use match , if Ok , future is excuted, if Error , continue. , 然后使用match ,如果Ok ,future 被执行,如果Error ,继续。 However, that is not work, if there is panic,.但是,如果有恐慌,那是行不通的。 still throw the panic?.仍然抛出恐慌? Do I have any ways to avoid the panic!我有什么办法可以避免恐慌! for this program?这个程序? Below is the code I do not using match!下面是我不使用匹配的代码!

async fn main() {
    let forever = task::spawn(async {
        let mut interval = interval(Duration::from_millis(60000));
        println!("Start");
        loop {
            interval.tick().await;
            call_data("name", "table").await;
        }
    });
    forever.await;
}

async fn call_data(name:&str, table: &str){
    data().unwrap();
}

This is the code I use match这是我使用match的代码

async fn main() {
        let forever = task::spawn(async {
            let mut interval = interval(Duration::from_millis(60000));
            println!("Start");
            loop {
                let a =call_data("BTC-USD", "test3").await;
                match a{
                      Ok=>(),
                      Err=>continue,
                 }
            }
        });
        forever.await;
    }
    
    async fn call_data(name:&str, table: &str){
        data().unwrap();
    }

Your fn call_data should look something like您的 fn call_data应该类似于

async fn call_data(name: &str, table: &str) -> std::result::Result<Data, Box<dyn error::Error>> {
    Ok(Data {})
}

and match should look likematch应该看起来像

match call_data("", "").await {
    Ok(data) => {
        // do something with data
    },
    Err(e) => println!("Error: {:?}", e),
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在 Wasm 发生恐慌之后触发 Rust Mutex 的释放,以便将来的调用正常? - How do I trigger the release of a Rust Mutex after a panic in Wasm so that future calls will be ok? 使用VS Code和MSVC工具链调试Rust程序时,如何在“ panic”处设置断点? - How can I set a breakpoint at a `panic` when debugging a Rust program with VS Code and the MSVC toolchain? 为什么在 Rust 程序中出现恐慌并期望 - Why getting panic in Rust program with an expect 在 Rust 如何转储恐慌的结果? 如果它不是 &amp;'static str,则由 join() 返回? - in Rust how can I dump the result of a panic! returned by join() if it is not a &'static str? 在删除 Rust Future 时恐慌运行异步代码 - Panic running async code while dropping Rust Future 怎么写恐慌! 像Rust中的宏? - How to write a panic! like macro in Rust? 如何在 rust 中等待 HashMap 未来值? - How can I await on HashMap future values in rust? 如果 function 调用在 rust 中处于 panic::catch_unwind 中,是否可以返回一个字符串? - Is it possible to return one String if the function call is in an panic::catch_unwind in rust? 如何避免由于线程崩溃而导致的死锁? - How to avoid a deadlock caused by a thread panic? 如何在线程上引起恐慌以立即结束主线程? - How can I cause a panic on a thread to immediately end the main thread?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM