我照书上写的代码:
function receiver (prod)
local status,value=coroutine.resume(prod)
return value
end
function send(x)
coroutine.yield(x)
end
function producer()
return coroutine.create(function()
while true do
local x=io.read()
print "ajdjd"
send(x)
end
end)
end
function filter(prod)
return coroutine.create(function()
for line=1,math.huge do
local x=receiver(prod)
x=string.format("%5d %s",line.x)
send(x)
end
end)
end
function consumer(prod)
while true do
local x=receiver(prod)
io.write(x,"\n")
end
end
在dofile了后,输入consumer(filter(producer()))后出现死循环输出cannot resume deadcoroutine,为什么io.read没让我输入,求解释,谢谢了
我用的是linux下的lua 5.0版本,见谅!
lua中dofile运行函数中的io.read(),怎么无法输入,输出出错
答案:5 悬赏:30 手机版
解决时间 2021-03-19 04:37
- 提问者网友:刺鸟
- 2021-03-18 21:53
最佳答案
- 五星知识达人网友:西风乍起
- 2021-03-18 22:15
我照书上写的代码:
function receiver (prod)
local status,value=coroutine.resume(prod)
return value
end
function send(x)
coroutine.yield(x)
end
function producer()
return coroutine.create(function()
while true do
local x=io.read()
print "ajdjd"
send(x)
end
end)
end
function filter(prod)
return coroutine.create(function()
for line=1,math.huge do
local x=receiver(prod)
x=string.format("%5d %s",line.x)
send(x)
end
end)
end
function consumer(prod)
while true do
local x=receiver(prod)
io.write(x,"\n")
end
end
在dofile了后,输入consumer(filter(producer()))后出现死循环输出cannot resume deadcoroutine,为什么io.read没让我输入,求解释,谢谢了我用的是linux下的lua 5.0版本,见谅!
function receiver (prod)
local status,value=coroutine.resume(prod)
return value
end
function send(x)
coroutine.yield(x)
end
function producer()
return coroutine.create(function()
while true do
local x=io.read()
print "ajdjd"
send(x)
end
end)
end
function filter(prod)
return coroutine.create(function()
for line=1,math.huge do
local x=receiver(prod)
x=string.format("%5d %s",line.x)
send(x)
end
end)
end
function consumer(prod)
while true do
local x=receiver(prod)
io.write(x,"\n")
end
end
在dofile了后,输入consumer(filter(producer()))后出现死循环输出cannot resume deadcoroutine,为什么io.read没让我输入,求解释,谢谢了我用的是linux下的lua 5.0版本,见谅!
全部回答
- 1楼网友:话散在刀尖上
- 2021-03-19 01:33
coroutine 是Lua 5.2+才有的
程序中肯定是出现了prod已经完成或返回(return),而你又要恢复(resume)执行prod代码导致这个错误.
使用 coroutine.status(prod) 测试一下哪一步已经为dead, 如果你还继续恢复(resume)执行那就出错了.
英文解释:
If we try to resume again a pair of values is returned: "cannot resume dead coroutine"
Once a coroutine exits or returns like a function it cannot be resumed.
- 2楼网友:等灯
- 2021-03-19 01:20
coroutine 是Lua 5.2+才有的
程序中肯定是出现了prod已经完成或返回(return),而你又要恢复(resume)执行prod代码导致这个错误.
使用 coroutine.status(prod) 测试一下哪一步已经为dead, 如果你还继续恢复(resume)执行那就出错了.
英文解释:
If we try to resume again a pair of values is returned: "cannot resume dead coroutine"
Once a coroutine exits or returns like a function it cannot be resumed.
- 3楼网友:酒醒三更
- 2021-03-19 00:09
coroutine 是Lua 5.2+才有的
程序中肯定是出现了prod已经完成或返回(return),而你又要恢复(resume)执行prod代码导致这个错误.
使用 coroutine.status(prod) 测试一下哪一步已经为dead, 如果你还继续恢复(resume)执行那就出错了.
英文解释:
If we try to resume again a pair of values is returned: "cannot resume dead coroutine"
Once a coroutine exits or returns like a function it cannot be resumed.
- 4楼网友:街头电车
- 2021-03-18 23:09
coroutine 是Lua 5.2+才有的
程序中肯定是出现了prod已经完成或返回(return),而你又要恢复(resume)执行prod代码导致这个错误.
使用 coroutine.status(prod) 测试一下哪一步已经为dead, 如果你还继续恢复(resume)执行那就出错了.
英文解释:
If we try to resume again a pair of values is returned: "cannot resume dead coroutine"
Once a coroutine exits or returns like a function it cannot be resumed.
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯