Lua嵌套协程

Alb*_*ard 4 lua nested coroutine

我试图使用Redis的-卢阿内库COPAS.它需要一些修补.一个问题是redis-lua将一些迭代器定义为协同程序,但这些迭代器执行可以执行的网络操作yield.

因此,coroutine.yield用于两个非常不同的东西:迭代器和copas.由于网络调用嵌套在迭代器中,coroutine.wrap因此迭代器拦截网络产量,而不是被copas拦截.

以下示例显示了问题:

local function iterator ()
  for i = 1, 2 do
    if i == 2 then coroutine.yield () end -- network yield
    coroutine.yield () -- iterator yield
  end
end
local citerator = coroutine.wrap (iterator)

local function loop () -- use of the iterator within a copas thread
  while citerator () do end
end
local cloop = coroutine.create (loop)

while coroutine.resume (cloop) do end -- same as copas loop, executes the cloop thread
Run Code Online (Sandbox Code Playgroud)

是否存在解决此问题的"标准"解决方案,仍允许使用协同程序进行迭代?

我能够通过"标记" yields(见下文)来制作一个小例子,但它与现有代码不兼容.我可以保留未修改的copas代码,但必须在redis-lua中更新迭代器.

local function wrap (f, my_tag)
  -- same as coroutine.wrap, but uses my_tag to yield again
  local co = coroutine.create (f)
  return function ()
    local t = table.pack (coroutine.resume (co))
    local code = t [1]
    local tag  = t [2]
    table.remove (t, 1)
    table.remove (t, 1)
    if tag == nil then
      return
    elseif my_tag == tag then
      return table.unpack (t)
    else
      coroutine.yield (tag, table.unpack (t))
    end
  end
end

local Iterator = {} -- tag for iterator yields
local Network  = {} -- tag for network yields

local function iterator ()
  for i = 1, 2 do
    if i == 2 then coroutine.yield (Network, i) end
    coroutine.yield (Iterator, i)
  end
end

local citerator = wrap (iterator, Iterator)

local function loop ()
  while citerator () do end
end

local cloop = wrap (loop, Network)

while cloop () do end
Run Code Online (Sandbox Code Playgroud)

有更好的解决方案吗?

sif*_*joe 5

Lua coroutine总是屈服于他们恢复的最后一个线程.Copas套接字函数期望返回到Copas事件循环,但是它们会被用于实现redis-lua迭代器的协程卡住.不幸的是,除了更改redis-lua迭代器的代码之外,你无法解决这个问题.没有人这样做的原因是,直到Lua 5.2(LuaJIT也可以这样做),甚至不可能从迭代器函数中产生(redis-lua中的迭代器产生工作正常,因为它们永远不会离开迭代器函数,但你不能for像Copas套接字函数那样产生超出循环的功能).

关于使用标记值来区分迭代器产量与其余部分的想法很好.你只需要确保你通过不用于迭代函数的协程一个级别的所有收益,包括任何参数/返回值coroutine.yieldcoroutine.resume(后来被隐式调用时coroutine.wrap编辑功能).

更具体地说,如果你在redis-lua中有这样的代码:

-- ...
return coroutine.wrap( function()
  -- ...
  while true do
    -- ...
    coroutine.yield( some_values )
  end
end )
Run Code Online (Sandbox Code Playgroud)

您将其更改为:

-- ...
local co_func = coroutine.wrap( function()
  -- ...
  while true do
    -- ...
    coroutine.yield( ITERATOR_TAG, some_values ) -- mark all iterator yields
  end
  return ITERATOR_TAG -- returns are also intended for the iterator
end )
return function()
  return pass_yields( co_func, co_func() ) -- initial resume of the iterator
end
Run Code Online (Sandbox Code Playgroud)

ITERATOR_TAGpass_yields功能的地方去附近的顶部redis.lua:

local ITERATOR_TAG = {} -- unique value to mark yields/returns

local function pass_yields( co_func, ... )
  if ... == ITERATOR_TAG then -- yield (or return) intended for iterator?
    return select( 2, ... ) -- strip the ITERATOR_TAG from results and return
  else
    -- pass other yields/resumes back and forth until we hit another iterator
    -- yield (or return); using tail recursion here instead of a loop makes
    -- handling vararg lists easier.
    return pass_yields( co_func, co_func( coroutine.yield( ... ) ) ) 
  end
end
Run Code Online (Sandbox Code Playgroud)

AFAIK,redis-lua开发人员计划在今年年底之前标记另一个版本,因此他们可能会对拉取请求感激不尽.