[英]How to propagate errors from multiple threads in rust?
In a Rust application that is:在 Rust 应用程序中:
I am propagating all errors up to the main thread, but I only see the Error that is hit by the main thread.我将所有错误传播到主线程,但我只看到主线程遇到的错误。 This usually happens before I join the child threads, so I don't see the actual root cause.
这通常发生在我加入子线程之前,所以我看不到实际的根本原因。
What minimum-boilerplate modification can I make to see the Errors from multiple threads?我可以进行哪些最小样板修改来查看来自多个线程的错误?
(I'll put some ideas I have in answers, but I'm hoping there is something better.) (我会把我的一些想法放在答案中,但我希望有更好的东西。)
I could use my main thread only for supervising child threads, aggregate all of their Results in some kind of Vec as I join them, filter it, and write custom code to print it.我可以只使用我的主线程来监督子线程,在我加入它们时将它们的所有结果聚合到某种 Vec 中,过滤它,并编写自定义代码来打印它。
This still feels like more work than should be necessary;感觉这仍然是不必要的工作。 I'm not the first person to write a threaded rust application that handles errors.
我不是第一个编写处理错误的线程 rust 应用程序的人。
I could let threads panic and "propagate the panics":我可以让线程恐慌并“传播恐慌”:
https://doc.rust-lang.org/beta/std/thread/type.Result.html https://doc.rust-lang.org/beta/std/thread/type.Result.html
But this is ugly:但这很丑陋:
One option would be to upgrade the entire application to use a logging framework, and then scrape through the logs.一种选择是升级整个应用程序以使用日志框架,然后搜索日志。
This will require modifications everywhere have added an anyhow.context(), ensure,, or bail.这将需要在任何地方进行修改,添加 anyhow.context()、ensure 或 bail。 annotation, to conditionally also throw a logger error.
注释,有条件地也抛出一个记录器错误。
Eventually I will want both logging/telemetry and clean teardown from propagating Results, but even then I will not want to have several lines of boilerplate for every single fallible function call.最终,我将希望记录/遥测和从传播结果中清除拆卸,但即便如此,我也不希望为每个可出错的 function 调用都有几行样板文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.