跳至主要內容

可恢复线程

ltpp-universe大约 1 分钟recoverable-spawnspawnrust

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

一个库,用于在 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

赞赏

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

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

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