跳至主要內容

可恢复线程池

ltpp-universe大约 1 分钟recoverable-thread-poolspawnrust

GITHUB 地址open in new window

LTPP-GIT 地址open in new window

目录

open in new window
open in new window

open in new window

官方文档open in new window

API 文档open in new window

一个支持自动从恐慌中恢复的线程池,允许线程在发生恐慌后重新启动。适用于网络和 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]

赞赏

作为这个项目的唯一开发者,我一直在努力确保后端服务器的稳定运行和服务的持续提供。这个项目凝聚了我的心血和热情,但服务器的硬件费用和网站维护确实是一个不小的负担。

如果您觉得这个项目对您有帮助,或希望支持一个坚持不懈的个人开发者,无论金额大小,您的赞助都将是对我的莫大鼓励。每一份支持都会直接用于服务器和维护成本,让这个项目能够继续为大家提供服务。感谢您的信任和支持!

上次编辑于:
贡献者: 尤雨东