当前位置: 首页 > news >正文

使用 Lua 协程模拟 Golang 的 go defer 编程模式

封装 go 函数

在 使用 Lua 协程处理异步回调函数 中已经介绍

这里简要列下:

  1. 封装 go 函数
    ---go 函数创建并启动一个协程
    ---@param _co_task function @函数原型 fun(_co:thread)
    function go(_co_task)local co = coroutine.create(_co_task) -- 创建的协程是暂停的coroutine.resume(co, co)              -- 调用 coroutine.resume 激活协程执行
    end
    
  2. 封装项目中异步函数
    ---封装 c_model.c_foo 异步函数,成为协程函数
    ---@param _co thread @协程对象
    ---@return boolean,string
    function co_foo(_co)c_model.c_foo(function(_ok, _result)coroutine.resume(_co, _ok, _result) -- 2. 回调函数被调用后,激活本协程继续执行。并把_ok, _result传递给 yieldend)return coroutine.yield()                -- 1. 主动放弃运行,本协程被切换出去
    end
    
  3. 使用例子
    ---test顺序编写代码,解决回调函数造成同块逻辑被撕裂的例子
    ---@param _co thread @协程对象
    function test(_co)for i = 1, 10, 1 dolocal ok, result = co_foo(_co) -- co_foo 会先 yield 切出;内部回调被执行时, resume 重新切回来继续执行print(ok, result)end
    end-- 启动 test 协程
    go(test)
    

封装 defer

defer 的特点有以下:

  • 协程正常退出能被执行
  • 协程异常退出能被执行
  • 同个协程内可以多次调用 defer
  • defer 被执行时,按出栈顺序被执行
defer 多次执行

首先定义 defer 函数,让它具备能多次被调用:

function defer(_co_wrap, h)table.insert(_co_wrap.defer_handlers, h)
end

因为要对 defer 的函数句柄做保持,以便退出时执行。包裹了下 co 对象:

---@class co_wrap
---@field co thread
---@field defer_handlers fun(_co_error:co_error)[]

同时定义下让 defer 的函数知道是否有错误的对象:

---@class co_error
---@field ok boolean
defer 被执行时,按出栈顺序被执行
function invoke_defer_handlers(_co_wrap, _co_error)for i=#_co_wrap.defer_handlers, 1, -1 dolocal h = _co_wrap.defer_handlers[i]xpcall(h, function(err) print(err) end, _co_error)end
end
协程异常时,能被执行

Lua 协程异常,通过 coroutine.resume 捕获,并返回错误信息

因此主要封装下 coroutine.resume :

function coroutine_resume(_co_wrap, ...)local ok, errmsg = coroutine.resume(_co_wrap.co, ...)if not ok theninvoke_defer_handlers(_co_wrap, {ok=false}) -- 异常退出end
end
协程正常退出时,能被执行
function go(_co_task)local co = coroutine.create(function(_co_wrap)_co_task(_co_wrap)invoke_defer_handlers(_co_wrap, {ok=true}) -- 正常退出end)local cowrap = { co = co, defer_handlers = {} } ---@type co_wrapcoroutine_resume(cowrap, cowrap) -- 初创建的协程是暂停的,手动触发执行
end

以上就可以在 Lua 中完全 Golang 的方式编写协程代码了

协程间通信

由于项目中暂时是一根线程管理一个 lua_state 对象,因此暂时无需求多线程中的协程间的通信需求

待续

http://www.lryc.cn/news/341676.html

相关文章:

  • 网络通信协议,UDP和TCP,初步了解
  • Golang | Leetcode Golang题解之第61题旋转链表
  • 美业SaaS系统多门店收银系统源码-【分润常见问题】讲解(一)
  • Chatbot 在教育中的应用
  • Apache和Nginx的区别以及如何选择
  • 深入探索Element-UI:构建高效Web前端的利器
  • 在Ubuntu 24.04 LTS (Noble Numbat)上安装nfs server以及nfs client
  • 供应链|经典论文解读:(s,S) 策略在动态库存下的最优性
  • Python从0到100(二十):文件读写和文件操作
  • AI+客服行业落地应用
  • 40 生产者消费者模型
  • QT5之windowswidget_菜单栏+工具栏_核心控件_浮动窗口_模态对话框_标准对话框/文本对话框
  • Satellite, Aerial, and Underwater Communication Track(WCSP2023)
  • AtCoder Regular Contest 176(ARC176)A、B
  • VTK —— 二、教程六 - 为模型加入3D微件(按下i键隐藏或显示)(附完整源码)
  • 一种基于图搜索的全局/局部路径避障策略
  • LT2611UX四端口 LVDS转 HDMI2.0,带音频
  • TypeError报错处理
  • PHP的数组练习实验
  • P3743 小鸟的设备
  • 数字旅游以科技创新为动力:推动旅游服务的智能化、网络化和个性化发展,满足游客日益增长的多元化、个性化需求
  • 64位的IP地址设想
  • 1.python爬虫爬取视频网站的视频可下载的源url
  • Linux目录结构
  • 电脑问题2【彻底删除CompatTelRunner】
  • 【算法】【贪心算法】【leetcode】870. 优势洗牌
  • Unity AVProVideo安卓播放视频问题
  • Redis使用手册之字符串
  • 嵌入式Linux学习第二天
  • 【intro】图卷积神经网络(GCN)