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 )
end
return ITERATOR_TAG
end )
return function()
return pass_yields( co_func, co_func() )
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.