python可以做网站模板吗,小影wordpress主题,网站的建设建议,赣州市做网站协程池
在 使用 Lua 协程模拟 Golang 的 go defer 编程模式 中介绍了 Lua 协程的使用#xff0c;模仿 golang 封装了下
还可以做进一步的优化
原来的 go 函数是这样实现的#xff1a;
function go(_co_task)local co coroutine.create(function(_co_wrap)_co_task(_co_w…协程池
在 使用 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, {oktrue}) -- 正常退出end)local cowrap { co co, defer_handlers {} } ---type co_wrapcoroutine_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_wrapif not co_wrap_0 thenlocal co coroutine.create(function()while true dolocal co_wrap_2, co_tesk_2 coroutine.yield()co_tesk_2(co_wrap_2)invoke_defer_handlers(_co_wrap, {oktrue}) -- 正常退出co_tesk_2 nilif #coroutine_pool coroutine_max_idle_num thencoroutine_pool[#coroutine_pool 1] { co co_wrap_2.co, defer_handlers {} }co_wrap_2 nilelseco_wrap_2 nilreturnendendend)continue.resume(co)local co_wrap_1 { co co, defer_handlers {} } ---type co_wrapcoroutine_resume(co_wrap_1, co_wrap_1, _co_task)else-- 复用协程coroutine_resume(co_wrap_0, co_wrap_0, _co_task)end
endgo 函数pop coroutine_pool 队尾如果不为空则有协程可以复用否则新建协程处理因为协程需要复用因此需要解耦 _co_task 可以通过 resume 传递过去还可以设置协程池大小。进而判断压入 coroutine_pool 继续 yield 等待下个任务还是直接结束本协程