Lua是否应该放弃协同程序,转向长尾词开发?
- 内容介绍
- 文章标签
- 相关推荐
本文共计423个文字,预计阅读时间需要2分钟。
在Lua 5.1中,若一个协程没有正常结束,那么在它恢复执行前,它会保留很多状态。例如,如果协程生成但由我来恢复,那么它在程序完成前可能会留下大量状态。这可以通过以下代码实现:`cor=coroutine.wrap(somefunc)`,然后使用 `while true do done=cor() end` 循环来不断恢复协程。
在Lua 5.1中,从不让一个协程正常结束有多糟糕?换句话说,如果一个协程产生但是我从未恢复它,它是否会在程序完成之前留下很多状态?cor=coroutine.wrap(somefunc) while true do done=cor() if done then -- coroutine exited with "return true" break else -- coroutine yielded with "coroutine.yield(false)" if some_condition then break end end end function somefunc() -- do something coroutine.yield(false) -- do some more return true end
根据上面伪代码中的some_condition,协程可能永远不会被恢复,因此可能永远不会正确地“结束”.
我可以这样做几十个协同程序而不必担心吗?将协同程序置于此状态是否安全?这个很贵吗?
垃圾收集器可以很容易地确定协程无法访问并收集它.我不知道是否有任何文件表明会发生这种情况,但我尝试了“经验方法”:while true do local cor = coroutine.wrap(function() coroutine.yield(false) end) cor() end
内存使用率并未随着时间的推移而增
编辑:谷歌说:
There is no explicit operation for deleting a Lua coroutine; like any other value in Lua, coroutines are discarded by garbage collection.(PDF中的第4页)
本文共计423个文字,预计阅读时间需要2分钟。
在Lua 5.1中,若一个协程没有正常结束,那么在它恢复执行前,它会保留很多状态。例如,如果协程生成但由我来恢复,那么它在程序完成前可能会留下大量状态。这可以通过以下代码实现:`cor=coroutine.wrap(somefunc)`,然后使用 `while true do done=cor() end` 循环来不断恢复协程。
在Lua 5.1中,从不让一个协程正常结束有多糟糕?换句话说,如果一个协程产生但是我从未恢复它,它是否会在程序完成之前留下很多状态?cor=coroutine.wrap(somefunc) while true do done=cor() if done then -- coroutine exited with "return true" break else -- coroutine yielded with "coroutine.yield(false)" if some_condition then break end end end function somefunc() -- do something coroutine.yield(false) -- do some more return true end
根据上面伪代码中的some_condition,协程可能永远不会被恢复,因此可能永远不会正确地“结束”.
我可以这样做几十个协同程序而不必担心吗?将协同程序置于此状态是否安全?这个很贵吗?
垃圾收集器可以很容易地确定协程无法访问并收集它.我不知道是否有任何文件表明会发生这种情况,但我尝试了“经验方法”:while true do local cor = coroutine.wrap(function() coroutine.yield(false) end) cor() end
内存使用率并未随着时间的推移而增
编辑:谷歌说:
There is no explicit operation for deleting a Lua coroutine; like any other value in Lua, coroutines are discarded by garbage collection.(PDF中的第4页)

