Lua nested coroutines

I am trying to use the redis-lua library in copas . This requires some corrections. One of the problems is that redis-lua defines some iterators as coroutines, but these iterators perform network operations that they can yield.

So, it is coroutine.yieldused for two very different things: for the iterator and for copas . Since network calls are nested in iterators, the network output is intercepted by the coroutine.wrapiterator, instead of being intercepted by copas .

The following example shows the problem:

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

Is there a “standard” solution to this problem that still allows coroutines for iterators to be used?

"" yield (. ), . 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

?

+4
2

Lua coroutine , . Copas Copas, , redis-lua. , , , redis-lua. , , , , Lua 5.2 (LuaJIT ), ( redis-lua , for, Copas).

- . , , , , / coroutine.yield coroutine.resume ( coroutine.wrap ed ).

, redis-lua:

-- ...
return coroutine.wrap( function()
  -- ...
  while true do
    -- ...
    coroutine.yield( some_values )
  end
end )

:

-- ...
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

ITERATOR_TAG pass_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

AFAIK, redis-lua , , , pull.

+5

wrap(loop). , copas 'wrap, copas...

copas.wrap() , loop !

. .

0

All Articles