Lua 协程池

协程池

使用 Lua 协程模拟 Golang 的 go defer 编程模式 中介绍了 Lua 协程的使用,模仿 golang 封装了下

还可以做进一步的优化

原来的 go 函数是这样实现的:

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_wrap
    coroutine_resume(cowrap)
end

go一次,均coroutine.create创建一根协程

阅读了 skynet 相关使用协程的代码,发现 skynet 使用了协程池

因此可以借鉴过来

具体实现

引入协程池的类似代码如下:

---@type co_wrap[]
local coroutine_pool = setmetatable({}, { __mode = "kv" })
local coroutine_max_idle_num = 10 -- 协程池,最大空闲个数

function go(_co_task)
    local co_wrap_0 = table.remove(coroutine_pool) ---@type co_wrap
    if not co_wrap_0 then
        local co = coroutine.create(function()
            while true do
                local co_wrap_2, co_tesk_2 = coroutine.yield()
                co_tesk_2(co_wrap_2)
                invoke_defer_handlers(_co_wrap, {ok=true}) -- 正常退出
                co_tesk_2 = nil
                if #coroutine_pool < coroutine_max_idle_num then
                    coroutine_pool[#coroutine_pool + 1] = { co = co_wrap_2.co, defer_handlers = {} }
                    co_wrap_2 = nil
                else
                    co_wrap_2 = nil
                    return
                end
            end
        end)
        continue.resume(co)
        local co_wrap_1 = { co = co, defer_handlers = {} } ---@type co_wrap
        coroutine_resume(co_wrap_1, co_wrap_1, _co_task)
    else
        -- 复用协程
        coroutine_resume(co_wrap_0, co_wrap_0, _co_task)
    end
end
  • go 函数,pop coroutine_pool 队尾,如果不为空,则有协程可以复用;否则新建协程处理
  • 因为协程需要复用,因此需要解耦 _co_task ,可以通过 resume 传递过去
  • 还可以设置协程池大小。进而判断,压入 coroutine_pool 继续 yield 等待下个任务;还是直接结束本协程

相关推荐

  1. Lua

    2024-05-12 07:22:08       13 阅读
  2. Lua-coroutine

    2024-05-12 07:22:08       35 阅读
  3. golang 实现

    2024-05-12 07:22:08       37 阅读
  4. go实现

    2024-05-12 07:22:08       17 阅读
  5. 【进程与线

    2024-05-12 07:22:08       6 阅读
  6. Golangants使用笔记

    2024-05-12 07:22:08       38 阅读
  7. 使用 Lua 处理异步回调函数

    2024-05-12 07:22:08       55 阅读
  8. Lua 模拟 Golang 的 go defer 编程模式

    2024-05-12 07:22:08       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-12 07:22:08       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-12 07:22:08       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-12 07:22:08       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-12 07:22:08       20 阅读

热门阅读

  1. EureKa详细讲解通俗易懂

    2024-05-12 07:22:08       10 阅读
  2. flask+layui显示监控视频

    2024-05-12 07:22:08       12 阅读
  3. 代码绘梦:Processing艺术编程入门

    2024-05-12 07:22:08       8 阅读
  4. 大数据调度 Apache Airflow 安装部署

    2024-05-12 07:22:08       15 阅读
  5. Mac 双网卡

    2024-05-12 07:22:08       15 阅读
  6. Spring AMQP的作用和用法

    2024-05-12 07:22:08       15 阅读
  7. 【数据结构】顺序栈

    2024-05-12 07:22:08       13 阅读
  8. C++基础——友元

    2024-05-12 07:22:08       12 阅读
  9. 【YOLOv9算法原理简介】

    2024-05-12 07:22:08       12 阅读
  10. unix C之环境变量

    2024-05-12 07:22:08       15 阅读
  11. react配置@指向src目录

    2024-05-12 07:22:08       10 阅读
  12. ActiViz中的图像平滑

    2024-05-12 07:22:08       12 阅读