0

0

如何实现Python重试超时装饰器

PHPz

PHPz

发布时间:2023-05-08 23:37:32

|

1686人浏览过

|

来源于亿速云

转载

    一、前言

    在写业务代码时候,有许多场景需要重试某块业务逻辑,例如网络请求、购物下单等,希望发生异常的时候多重试几次。

    二、简单分析

    一个重试装饰器,最重要的就是发生意外异常处理失败自动重试,有如下几点需要注意

    • 失败不能一直重试,因为可能会出现死循环浪费资源,因此需要有 最大重试次数 或者 最大超时时间

    • 不能重试太频繁,因为太频繁容易导致重试次数很快用完,却没有成功响应,需要有 重试时间间隔 来限制,有时可以加大成功概率,例如网络请求时有一段时间是堵塞的,或者对方服务负载太高导致一段时间无法响应等。

    简单分析完,我们的重试装饰器,就要支持可配置最大重试次数、最大超时时间、重试间隔,所以装饰器就要设计成带参数装饰器。

    立即学习Python免费学习笔记(深入)”;

    三、代码模拟实现

    重试装饰器-初版

    分析完毕后,看看第一版的装饰器

    import time
    from functools import wraps
    def task_retry(max_retry_count: int = 5, time_interval: int = 2):
        """
        任务重试装饰器
        Args:
            max_retry_count: 最大重试次数 默认5次
            time_interval: 每次重试间隔 默认2s
        """
        def _task_retry(task_func):
            @wraps(task_func)
            def wrapper(*args, **kwargs):
                # 函数循环重试
                for retry_count in range(max_retry_count):
                    print(f"execute count {retry_count + 1}")
                    try:
                        task_result = task_func(*args, **kwargs)
                        return task_result
                    except Exception as e:
                        print(f"fail {str(e)}")
                        time.sleep(time_interval)
            return wrapper
        return _task_retry

    装饰器内部闭包,就简单通过 for 循环 来执行指定重试次数,成功获取结果就直接 return 返回,发生异常则睡眠配置重试间隔时间后继续循环

    写个例子来模拟测试下看看效果

    没有异常正常执行,在函数中模拟一个异常来进行重试看看

    @task_retry(max_retry_count=3, time_interval=1)
    def user_place_order():
        a = 1 / 0
        print("user place order success")
        return {"code": 0, "msg": "ok"}
    ret = user_place_order()
    print("user place order ret", ret)
    >>>out
    fail division by zero
    execute count 2
    fail division by zero
    execute count 3
    fail division by zero
    user place order ret None

    可以看到 user_place_order 函数执行了三遍,都发生了除零异常,最后超过最大执行次数,返回了 None 值,我们可以在主逻辑中来判断返回值是否为 None 来进行超过最大重试次数失败的业务逻辑处理

    ret = user_place_order()
    print("user place order ret", ret)
    
    if not ret:
        print("user place order failed")
        ...

    重试装饰器-改进版

    现在只能配置 最大重试次数 没有最大超时时间,有时候我们想不但有重试,还得在规定时间内完成,不想浪费太多试错时间。所以增加一个 最大超时时间配置选项默认为None,有值时超过最大超时时间退出重试。

    def task_retry(max_retry_count: int = 5, time_interval: int = 2, max_timeout: int = None):
        """
        任务重试装饰器
        Args:
            max_retry_count: 最大重试次数 默认 5 次
            time_interval: 每次重试间隔 默认 2s
            max_timeout: 最大超时时间,单位s 默认为 None,
        """
    
        def _task_retry(task_func):
    
            @wraps(task_func)
            def wrapper(*args, **kwargs):
                # 函数循环重试
                start_time = time.time()
                for retry_count in range(max_retry_count):
                    print(f"execute count {retry_count + 1}")
                    use_time = time.time() - start_time
                    if max_timeout and use_time > max_timeout:
                        # 超出最大超时时间
                        print(f"execute timeout, use time {use_time}s, max timeout {max_timeout}")
                        return
    
                    try:
                        return task_func(*args, **kwargs)
                    except Exception as e:
                        print(f"fail {str(e)}")
                        time.sleep(time_interval)
    
            return wrapper
    
        return _task_retry

    看看效果

    卡拉OK视频制作
    卡拉OK视频制作

    卡拉OK视频制作,在几分钟内制作出你的卡拉OK视频

    下载
    # 超时
    @task_retry(max_retry_count=3, time_interval=1, max_timeout=2)
    def user_place_order():
        a = 1 / 0
        print("user place order success")
        return {"code": 0, "msg": "ok"}
    >>>out
    execute count 1
    fail division by zero
    execute count 2
    fail division by zero
    execute count 3
    execute timeout, use time 2.010528802871704s, max timeout 2
    user place order ret None
    # 超过最大重试次数
    @task_retry(max_retry_count=3, time_interval=1)
    def user_place_order():
        a = 1 / 0
        print("user place order success")
        return {"code": 0, "msg": "ok"}
    >>>out
    execute count 1
    fail division by zero
    execute count 2
    fail division by zero
    execute count 3
    fail division by zero
    user place order ret None
    # 正常
    @task_retry(max_retry_count=3, time_interval=1, max_timeout=2)
    def user_place_order():
        # a = 1 / 0
        print("user place order success")
        return {"code": 0, "msg": "ok"}
    >>>out
    execute count 1
    user place order success
    user place order ret {'code': 0, 'msg': 'ok'}

    重试装饰器-加强版

    到这重试装饰器基本功能就实现了,但还可以加强,Python现在支持 async 异步方式写法,因此要是可以兼容异步写法那就更好了。先看看装饰异步函数会是什么样的效果

    import time
    import asyncio
    import functools
    def task_retry(max_retry_count: int = 5, time_interval: int = 2, max_timeout: int = None):
        """
        任务重试装饰器
        Args:
            max_retry_count: 最大重试次数 默认 5 次
            time_interval: 每次重试间隔 默认 2s
            max_timeout: 最大超时时间,单位s 默认为 None,
        """
        def _task_retry(task_func):
            @wraps(task_func)
            def wrapper(*args, **kwargs):
                # 函数循环重试
                start_time = time.time()
                for retry_count in range(max_retry_count):
                    print(f"execute count {retry_count + 1}")
                    use_time = time.time() - start_time
                    if max_timeout and use_time > max_timeout:
                        # 超出最大超时时间
                        print(f"execute timeout, use time {use_time}s, max timeout {max_timeout}")
                        return
                    try:
                        return task_func(*args, **kwargs)
                    except Exception as e:
                        print(f"fail {str(e)}")
                        time.sleep(time_interval)
            return wrapper
        return _task_retry
    @task_retry(max_retry_count=3, time_interval=1, max_timeout=2)
    def user_place_order():
        # a = 1 / 0
        print("user place order success")
        return {"code": 0, "msg": "ok"}
    @task_retry(max_retry_count=3, time_interval=2, max_timeout=5)
    async def user_place_order_async():
        """异步函数重试案例"""
        a = 1 / 0
        print("user place order success")
        return {"code": 0, "msg": "ok"}
    async def main():
        # 同步案例
        # ret = user_place_order()
        # print(f"user place order ret {ret}")
        # 异步案例
        ret = await user_place_order_async()
        print(f"user place order ret {ret}")
    if __name__ == '__main__':
        asyncio.run(main())
    # 正常时候
    execute count 1
    user place order success
    user place order ret {'code': 0, 'msg': 'ok'}
    # 异常时候
    >>>out
    execute count 1
    Traceback (most recent call last):
      File "G:/code/python/py-tools/decorator/base.py", line 138, in <module>
        asyncio.run(main())
      File "G:\softs\DevEnv\python-3.7.9\lib\asyncio\runners.py", line 43, in run
        return loop.run_until_complete(main)
      File "G:\softs\DevEnv\python-3.7.9\lib\asyncio\base_events.py", line 587, in run_until_complete
        return future.result()
      File "G:/code/python/py-tools/decorator/base.py", line 133, in main
        ret = await user_place_order_async()
      File "G:/code/python/py-tools/decorator/base.py", line 121, in user_place_order_async
        a = 1 / 0
    ZeroDivisionError: division by zero
    Process finished with exit code 1

    发现发生异常的时候并没有重试,为什么呢?其实在执行 task_func() 它并没有真正的执行内部逻辑,而是返回一个 coroutine 协程对象,并不会报异常,所以再装饰器中执行一遍就成功就出来了,外面 ret = await user_place_order_async(), 后才真正的等待执行,然后执行函数内的逻辑再报异常就没有捕获到。我们可以打断点验证下

    如何实现Python重试超时装饰器

    这样装饰器就不支持异步函数的重试,需要加强它,可以使用 asyncio.iscoroutinefunction() 来进行异步函数的判断, 然后再加一个异步函数的闭包就可以实现异步、同步函数都兼容的重试装饰器。

    def task_retry(max_retry_count: int = 5, time_interval: int = 2, max_timeout: int = None):
        """
        任务重试装饰器
        Args:
            max_retry_count: 最大重试次数 默认 5 次
            time_interval: 每次重试间隔 默认 2s
            max_timeout: 最大超时时间,单位s 默认为 None,
        """
    
        def _task_retry(task_func):
    
            @functools.wraps(task_func)
            def sync_wrapper(*args, **kwargs):
                # 同步循环重试
                start_time = time.time()
                for retry_count in range(max_retry_count):
                    print(f"execute count {retry_count + 1}")
                    use_time = time.time() - start_time
                    if max_timeout and use_time > max_timeout:
                        # 超出最大超时时间
                        print(f"execute timeout, use time {use_time}s, max timeout {max_timeout}")
                        return
    
                    try:
                        task_ret = task_func(*args, **kwargs)
                        return task_ret
                    except Exception as e:
                        print(f"fail {str(e)}")
                        time.sleep(time_interval)
    
            @functools.wraps(task_func)
            async def async_wrapper(*args, **kwargs):
                # 异步循环重试
                start_time = time.time()
                for retry_count in range(max_retry_count):
                    print(f"execute count {retry_count + 1}")
                    use_time = time.time() - start_time
                    if max_timeout and use_time > max_timeout:
                        # 超出最大超时时间
                        print(f"execute timeout, use time {use_time}s, max timeout {max_timeout}")
                        return
    
                    try:
                        return await task_func(*args, **kwargs)
                    except Exception as e:
                        print(f"fail {str(e)}")
                        await asyncio.sleep(time_interval)
    
            # 异步函数判断
            wrapper_func = async_wrapper if asyncio.iscoroutinefunction(task_func) else sync_wrapper
            return wrapper_func
    
        return _task_retry

    注意时间等待 await asyncio.sleep(time_interval) 会导致函数挂起,程序不会在这里等待,而是去事件循环loop中执行其他的已经就绪的任务,如果其他函数运行时间太久了才切换回来,会导致时间超时,换成 time.sleep()的话其实也没有用,如果函数内部还有异步函数执行还是会切换出去,因此异步的时候感觉超时参数意义不大。

    模拟测试下

    @task_retry(max_retry_count=5, time_interval=2, max_timeout=5)
    async def user_place_order_async():
        """异步函数重试案例"""
        a = 1 / 0
        print("user place order success")
        return {"code": 0, "msg": "ok"}
    
    
    async def io_test():
        """模拟io阻塞"""
        print("io test start")
        time.sleep(3)
        print("io test end")
        return "io test end"
    
    
    async def main():
        # 同步案例
        # ret = user_place_order()
        # print(f"user place order ret {ret}")
    
        # 异步案例
        # ret = await user_place_order_async()
        # print(f"user place order ret {ret}")
    
        # 并发异步
        order_ret, io_ret = await asyncio.gather(
            user_place_order_async(),
            io_test(),
        )
        print(f"io ret {io_ret}")
        print(f"user place order ret {order_ret}")
    
    
    if __name__ == '__main__':
        asyncio.run(main())
        
        
     >>>out
    execute count 1
    fail division by zero
    io test start
    io test end
    execute count 2
    fail division by zero
    execute count 3
    execute timeout, use time 5.015768527984619s, max timeout 5
    io ret io test end
    user place order ret None

    可以看出执行一遍后自动切换到了 io_test 中执行由于 io test 中的 time.sleep(3) 会导致整个线程阻塞,一定要等到io_test执行完后才会切换回去,然后再执行两遍就超时了,你可能会说都用异步的库,是的异步的库是可以加速,但我想表达就是这时候统计的耗时是整个程序的而不是单独一个函数的。大家可以在评论区帮我想想有没有其他的方法,要么就不要用这个超时参数。

    可以兼容异步函数、然后超时参数可以不配置,影响不大,O(∩_∩)O~

    重试装饰器-最终版

    最终版就是利用抛异常的方式来结束超过最大重试次数、最大超时,而不是直接返回None,然后再添加一个可配置捕获指定异常的参数,当发生特定异常的时候才重试。

    import time
    import asyncio
    import functools
    from typing import Type
    
    
    class MaxRetryException(Exception):
        """最大重试次数异常"""
        pass
    
    
    class MaxTimeoutException(Exception):
        """最大超时异常"""
        pass
    
    
    def task_retry(
            max_retry_count: int = 5,
            time_interval: int = 2,
            max_timeout: int = None,
            catch_exc: Type[BaseException] = Exception
    ):
        """
        任务重试装饰器
        Args:
            max_retry_count: 最大重试次数 默认 5 次
            time_interval: 每次重试间隔 默认 2s
            max_timeout: 最大超时时间,单位s 默认为 None,
            catch_exc: 指定捕获的异常类用于特定的异常重试 默认捕获 Exception
        """
    
        def _task_retry(task_func):
    
            @functools.wraps(task_func)
            def sync_wrapper(*args, **kwargs):
                # 函数循环重试
                start_time = time.time()
                for retry_count in range(max_retry_count):
                    print(f"execute count {retry_count + 1}")
                    use_time = time.time() - start_time
                    if max_timeout and use_time > max_timeout:
                        # 超出最大超时时间
                        raise MaxTimeoutException(f"execute timeout, use time {use_time}s, max timeout {max_timeout}")
    
                    try:
                        task_ret = task_func(*args, **kwargs)
                        return task_ret
                    except catch_exc as e:
                        print(f"fail {str(e)}")
                        time.sleep(time_interval)
                else:
                    # 超过最大重试次数, 抛异常终止
                    raise MaxRetryException(f"超过最大重试次数失败, max_retry_count {max_retry_count}")
    
            @functools.wraps(task_func)
            async def async_wrapper(*args, **kwargs):
                # 异步循环重试
                start_time = time.time()
                for retry_count in range(max_retry_count):
                    print(f"execute count {retry_count + 1}")
                    use_time = time.time() - start_time
                    if max_timeout and use_time > max_timeout:
                        # 超出最大超时时间
                        raise MaxTimeoutException(f"execute timeout, use time {use_time}s, max timeout {max_timeout}")
    
                    try:
                        return await task_func(*args, **kwargs)
                    except catch_exc as e:
                        print(f"fail {str(e)}")
                        await asyncio.sleep(time_interval)
                else:
                    # 超过最大重试次数, 抛异常终止
                    raise MaxRetryException(f"超过最大重试次数失败, max_retry_count {max_retry_count}")
    
            # 异步函数判断
            wrapper_func = async_wrapper if asyncio.iscoroutinefunction(task_func) else sync_wrapper
            return wrapper_func
    
        return _task_retry
    
    
    @task_retry(max_retry_count=3, time_interval=1, catch_exc=ZeroDivisionError,max_timeout=5)
    def user_place_order():
        a = 1 / 0
        print("user place order success")
        return {"code": 0, "msg": "ok"}
    
    
    @task_retry(max_retry_count=5, time_interval=2, max_timeout=5)
    async def user_place_order_async():
        """异步函数重试案例"""
        a = 1 / 0
        print("user place order success")
        return {"code": 0, "msg": "ok"}
    
    
    async def io_test():
        """模拟io阻塞"""
        print("io test start")
        time.sleep(3)
        print("io test end")
        return "io test end"
    
    
    async def main():
        # 同步案例
        try:
            ret = user_place_order()
            print(f"user place order ret {ret}")
        except MaxRetryException as e:
            # 超过最大重试次数处理
            print("MaxRetryException", e)
        except MaxTimeoutException as e:
            # 超过最大超时处理
            print("MaxTimeoutException", e)
    
        # 异步案例
        # ret = await user_place_order_async()
        # print(f"user place order ret {ret}")
    
        # 并发异步
        # order_ret, io_ret = await asyncio.gather(
        #     user_place_order_async(),
        #     io_test(),
        # )
        # print(f"io ret {io_ret}")
        # print(f"user place order ret {order_ret}")
    
    
    if __name__ == '__main__':
        asyncio.run(main())

    测试捕获指定异常

    # 指定捕获除零错误,正常捕获重试
    @task_retry(max_retry_count=3, time_interval=1, catch_exc=ZeroDivisionError)
    def user_place_order():
        a = 1 / 0
        # a = []
        # b = a[0]
        print("user place order success")
        return {"code": 0, "msg": "ok"}
     
    
    # out
    execute count 1
    fail division by zero
    execute count 2
    fail division by zero
    execute count 3
    fail division by zero
    MaxRetryException 超过最大重试次数失败, max_retry_count 3
    
    
    # 指定捕获除零错误,报索引越界错误,未正常捕获重试,直接退出
    @task_retry(max_retry_count=3, time_interval=1, catch_exc=ZeroDivisionError)
    def user_place_order():
        # a = 1 / 0
        a = []
        b = a[0]
        print("user place order success")
        return {"code": 0, "msg": "ok"}
     
    
    # out
    Traceback (most recent call last):
      File "G:/code/python/py-tools/decorator/base.py", line 184, in <module>
        asyncio.run(main())
      File "G:\softs\DevEnv\python-3.7.9\lib\asyncio\runners.py", line 43, in run
        return loop.run_until_complete(main)
      File "G:\softs\DevEnv\python-3.7.9\lib\asyncio\base_events.py", line 587, in run_until_complete
        return future.result()
      File "G:/code/python/py-tools/decorator/base.py", line 161, in main
        ret = user_place_order()
      File "G:/code/python/py-tools/decorator/base.py", line 97, in sync_wrapper
        task_ret = task_func(*args, **kwargs)
      File "G:/code/python/py-tools/decorator/base.py", line 137, in user_place_order
        b = a[0]
    IndexError: list index out of range
    
    Process finished with exit code 1

    修改记录

    • 把重试里的超时计算单独抽离出来,这样功能不会太藕合,分两个装饰实现

    def set_timeout(timeout: int, use_signal=False):
        """
        超时处理装饰器
        Args:
            timeout: 超时时间,单位秒
            use_signal: 使用信号量机制只能在 unix内核上使用,默认False
        Raises:
            TimeoutException
        """
        def _timeout(func: Callable):
            def _handle_timeout(signum, frame):
                raise MaxTimeoutException(f"Function timed out after {timeout} seconds")
            @functools.wraps(func)
            def sync_wrapper(*args, **kwargs):
                # 同步函数处理超时
                if use_signal:
                    # 使用信号量计算超时
                    signal.signal(signal.SIGALRM, _handle_timeout)
                    signal.alarm(timeout)
                    try:
                        return func(*args, **kwargs)
                    finally:
                        signal.alarm(0)
                else:
                    # 使用线程
                    with ThreadPoolExecutor() as executor:
                        future = executor.submit(func, *args, **kwargs)
                        try:
                            return future.result(timeout)
                        except TimeoutError:
                            raise MaxTimeoutException(f"Function timed out after {timeout} seconds")
            @functools.wraps(func)
            async def async_wrapper(*args, **kwargs):
                # 异步函数处理超时
                try:
                    ret = await asyncio.wait_for(func(*args, **kwargs), timeout)
                    return ret
                except asyncio.TimeoutError:
                    raise MaxTimeoutException(f"Function timed out after {timeout} seconds")
            return async_wrapper if asyncio.iscoroutinefunction(func) else sync_wrapper
        return _timeout
    def retry(
            max_count: int = 5,
            interval: int = 2,
            catch_exc: Type[BaseException] = Exception
    ):
        """
        重试装饰器
        Args:
            max_count: 最大重试次数 默认 5 次
            interval: 每次异常重试间隔 默认 2s
            catch_exc: 指定捕获的异常类用于特定的异常重试 默认捕获 Exception
        Raises:
            MaxRetryException
        """
        def _retry(task_func):
            @functools.wraps(task_func)
            def sync_wrapper(*args, **kwargs):
                # 函数循环重试
                for retry_count in range(max_count):
                    logger.info(f"{task_func} execute count {retry_count + 1}")
                    try:
                        return task_func(*args, **kwargs)
                    except catch_exc:
                        logger.error(f"fail {traceback.print_exc()}")
                        if retry_count < max_count - 1:
                            # 最后一次异常不等待
                            time.sleep(interval)
                # 超过最大重试次数, 抛异常终止
                raise MaxRetryException(f"超过最大重试次数失败, max_retry_count {max_count}")
            @functools.wraps(task_func)
            async def async_wrapper(*args, **kwargs):
                # 异步循环重试
                for retry_count in range(max_count):
                    logger.info(f"{task_func} execute count {retry_count + 1}")
                    try:
                        return await task_func(*args, **kwargs)
                    except catch_exc as e:
                        logger.error(f"fail {str(e)}")
                        if retry_count < max_count - 1:
                            await asyncio.sleep(interval)
                # 超过最大重试次数, 抛异常终止
                raise MaxRetryException(f"超过最大重试次数失败, max_retry_count {max_count}")
            # 异步函数判断
            wrapper_func = async_wrapper if asyncio.iscoroutinefunction(task_func) else sync_wrapper
            return wrapper_func
        return _retry

    相关文章

    python速学教程(入门到精通)
    python速学教程(入门到精通)

    python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

    下载

    本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

    热门AI工具

    更多
    DeepSeek
    DeepSeek

    幻方量化公司旗下的开源大模型平台

    豆包大模型
    豆包大模型

    字节跳动自主研发的一系列大型语言模型

    通义千问
    通义千问

    阿里巴巴推出的全能AI助手

    腾讯元宝
    腾讯元宝

    腾讯混元平台推出的AI助手

    文心一言
    文心一言

    文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

    讯飞写作
    讯飞写作

    基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

    即梦AI
    即梦AI

    一站式AI创作平台,免费AI图片和视频生成。

    ChatGPT
    ChatGPT

    最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

    相关专题

    更多
    线程和进程的区别
    线程和进程的区别

    线程和进程的区别:线程是进程的一部分,用于实现并发和并行操作,而线程共享进程的资源,通信更方便快捷,切换开销较小。本专题为大家提供线程和进程区别相关的各种文章、以及下载和课程。

    743

    2023.08.10

    go语言闭包相关教程大全
    go语言闭包相关教程大全

    本专题整合了go语言闭包相关数据,阅读专题下面的文章了解更多相关内容。

    151

    2025.07.29

    Rust内存安全机制与所有权模型深度实践
    Rust内存安全机制与所有权模型深度实践

    本专题围绕 Rust 语言核心特性展开,深入讲解所有权机制、借用规则、生命周期管理以及智能指针等关键概念。通过系统级开发案例,分析内存安全保障原理与零成本抽象优势,并结合并发场景讲解 Send 与 Sync 特性实现机制。帮助开发者真正理解 Rust 的设计哲学,掌握在高性能与安全性并重场景中的工程实践能力。

    2

    2026.03.05

    PHP高性能API设计与Laravel服务架构实践
    PHP高性能API设计与Laravel服务架构实践

    本专题围绕 PHP 在现代 Web 后端开发中的高性能实践展开,重点讲解基于 Laravel 框架构建可扩展 API 服务的核心方法。内容涵盖路由与中间件机制、服务容器与依赖注入、接口版本管理、缓存策略设计以及队列异步处理方案。同时结合高并发场景,深入分析性能瓶颈定位与优化思路,帮助开发者构建稳定、高效、易维护的 PHP 后端服务体系。

    58

    2026.03.04

    AI安装教程大全
    AI安装教程大全

    2026最全AI工具安装教程专题:包含各版本AI绘图、AI视频、智能办公软件的本地化部署手册。全篇零基础友好,附带最新模型下载地址、一键安装脚本及常见报错修复方案。每日更新,收藏这一篇就够了,让AI安装不再报错!

    30

    2026.03.04

    Swift iOS架构设计与MVVM模式实战
    Swift iOS架构设计与MVVM模式实战

    本专题聚焦 Swift 在 iOS 应用架构设计中的实践,系统讲解 MVVM 模式的核心思想、数据绑定机制、模块拆分策略以及组件化开发方法。内容涵盖网络层封装、状态管理、依赖注入与性能优化技巧。通过完整项目案例,帮助开发者构建结构清晰、可维护性强的 iOS 应用架构体系。

    59

    2026.03.03

    C++高性能网络编程与Reactor模型实践
    C++高性能网络编程与Reactor模型实践

    本专题围绕 C++ 在高性能网络服务开发中的应用展开,深入讲解 Socket 编程、多路复用机制、Reactor 模型设计原理以及线程池协作策略。内容涵盖 epoll 实现机制、内存管理优化、连接管理策略与高并发场景下的性能调优方法。通过构建高并发网络服务器实战案例,帮助开发者掌握 C++ 在底层系统与网络通信领域的核心技术。

    25

    2026.03.03

    Golang 测试体系与代码质量保障:工程级可靠性建设
    Golang 测试体系与代码质量保障:工程级可靠性建设

    Go语言测试体系与代码质量保障聚焦于构建工程级可靠性系统。本专题深入解析Go的测试工具链(如go test)、单元测试、集成测试及端到端测试实践,结合代码覆盖率分析、静态代码扫描(如go vet)和动态分析工具,建立全链路质量监控机制。通过自动化测试框架、持续集成(CI)流水线配置及代码审查规范,实现测试用例管理、缺陷追踪与质量门禁控制,确保代码健壮性与可维护性,为高可靠性工程系统提供质量保障。

    79

    2026.02.28

    Golang 工程化架构设计:可维护与可演进系统构建
    Golang 工程化架构设计:可维护与可演进系统构建

    Go语言工程化架构设计专注于构建高可维护性、可演进的企业级系统。本专题深入探讨Go项目的目录结构设计、模块划分、依赖管理等核心架构原则,涵盖微服务架构、领域驱动设计(DDD)在Go中的实践应用。通过实战案例解析接口抽象、错误处理、配置管理、日志监控等关键工程化技术,帮助开发者掌握构建稳定、可扩展Go应用的最佳实践方法。

    61

    2026.02.28

    热门下载

    更多
    网站特效
    /
    网站源码
    /
    网站素材
    /
    前端模板

    精品课程

    更多
    相关推荐
    /
    热门推荐
    /
    最新课程
    最新Python教程 从入门到精通
    最新Python教程 从入门到精通

    共4课时 | 22.5万人学习

    Django 教程
    Django 教程

    共28课时 | 4.7万人学习

    SciPy 教程
    SciPy 教程

    共10课时 | 1.8万人学习

    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送

    Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号