可恢复线程池
大约 1 分钟recoverable-thread-poolspawnrust
目录
一个支持自动从恐慌中恢复的线程池,允许线程在发生恐慌后重新启动。适用于网络和 Web 编程中的高容错并发场景。
安装
要使用此 crate,可以运行以下命令:
cargo add recoverable-thread-pool
使用示例
同步
use recoverable_thread_pool::*;
use std::{thread::sleep, time::Duration};
let thread_pool: ThreadPool = ThreadPool::new(1);
let first_res: SendResult = thread_pool.execute(|| {
println!("first");
});
println!("{:?}", first_res);
let panic_res: SendResult = thread_pool.execute_with_catch(
|| {
panic!("[panic]");
},
|err| {
println!("Catch panic {}", err);
},
);
println!("{:?}", panic_res);
let second_res: SendResult = thread_pool.execute_with_catch_finally(
|| {
panic!("[panic]");
},
|_err| {
panic!("[panic]");
},
|| {
println!("finally");
},
);
println!("{:?}", second_res);
sleep(Duration::from_secs(10));
异步
use recoverable_thread_pool::*;
use std::{thread::sleep, time::Duration};
let thread_pool: ThreadPool = ThreadPool::new(1);
let first_res: SendResult = thread_pool.async_execute(|| async {
println!("first");
});
println!("{:?}", first_res);
let panic_res: SendResult = thread_pool.async_execute_with_catch(
|| async {
panic!("[panic]");
},
|err| async move {
println!("Catch panic {}", err);
},
);
println!("{:?}", panic_res);
let second_res: SendResult = thread_pool.async_execute_with_catch_finally(
|| async {
panic!("[panic]");
},
|_err| async {
panic!("[panic]");
},
|| async {
println!("finally");
},
);
println!("{:?}", second_res);
sleep(Duration::from_secs(10));
许可证
此项目基于 MIT 许可证。有关详细信息,请参阅 LICENSE 文件。
贡献
欢迎贡献!请提交 issue 或 pull request。
联系方式
如有任何问题,请通过以下邮箱联系作者:ltpp-universe [email protected]。