我正在尝试在copas中使用redis-lua库,但需要进行一些修补。其中一个问题是redis-lua将一些迭代器定义为协程,但这些迭代器执行可以
因此,
下面的示例显示了问题:
yield
的网络操作。因此,
coroutine.yield
用于两个非常不同的事情:用于迭代器和copas。由于网络调用嵌套在迭代器中,网络yield被迭代器的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
是否有一种“标准”解决方案来解决这个问题,同时仍然允许使用协程作为迭代器?
我能够通过“标记”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
有更好的解决方案吗?
coroutine
。它通过了lua测试套件。唯一的变化是将协程模块作为以下方式进行引用:local coroutine = require "coroutine.make"()
。 - Alban Linardtable.foreach
)为使用协程和标记 yield 的for
循环迭代器。 - siffiejoe