可恢复线程
大约 1 分钟recoverable-spawnspawnrust
目录
一个库,用于在 panic 后自动重启线程。它在发生意外错误时,确保线程继续运行。它提供了一种简单有效的机制,捕获 panic,重启线程,并可以选择性地记录错误,用于监控和调试。
安装
要使用此库,可以运行以下命令:
cargo add recoverable-spawn
使用
recoverable_spawn
use recoverable_spawn::*;
let msg: &str = "test";
let handle: JoinHandle<()> = recoverable_spawn(move || {
panic!("{}", msg);
});
let _ = handle.join();
let handle: JoinHandle<()> = recoverable_spawn_with_error_handle(
move || {
panic!("{}", msg);
},
|err| {
println!("handle error => {}", err);
},
);
let _ = handle.join();
recoverable_spawn_with_error_handle
use recoverable_spawn::*;
let msg: &str = "test";
let handle: JoinHandle<()> = recoverable_spawn_with_error_handle(
move || {
panic!("{}", msg);
},
|err| {
println!("handle error => {}", err);
},
);
let _ = handle.join();
async_recoverable_spawn
use recoverable_spawn::*;
let msg: &str = "test";
let handle: JoinHandle<()> = async_recoverable_spawn(move || async move {
panic!("{}", msg);
});
let _ = handle.join();
async_recoverable_spawn_catch
use recoverable_spawn::*;
let msg: &str = "test";
let handle: JoinHandle<()> = async_recoverable_spawn_catch(
move || async move {
panic!("{}", msg);
},
move |err| async move {
println!("handle error => {}", err);
},
);
let _ = handle.join();
test_async_recoverable_spawn_catch_finally
use recoverable_spawn::*;
let msg: &str = "test";
let handle: JoinHandle<()> = test_async_recoverable_spawn_catch_finally(
move || async move {
panic!("{}", msg);
},
move |err| async move {
println!("handle error => {}", err);
panic!("{}", err);
},
move || async move {
println!("finally");
},
);
let _ = handle.join();
许可证
此项目使用 MIT 许可证。详细信息请参见 LICENSE 文件。
贡献
欢迎贡献!请提交问题或拉取请求。
联系
如有任何疑问,请联系作者:ltpp-universe root@ltpp.vip。