使用Lua io.popen执行对地址的ping,如果ping超时,则为非阻塞。

在这个优秀的页面上找到了下面的函数:

function os.capture(cmd, raw)
  local f = assert(io.popen(cmd, 'r'))
  local s = assert(f:read('*a'))
  f:close()
  if raw then return s end
  s = string.gsub(s, '^%s+', '')
  s = string.gsub(s, '%s+$', '')
  s = string.gsub(s, '[\n\r]+', ' ')
  return s
end

如果我使用 cmd = "ping myknowngoodip -c 1" 执行此函数,则一切正常,它会立即返回结果。

但是如果ping超时(服务器宕机或其他原因),则整个程序将阻塞至少5秒钟。

我的可能解决方案:

  • 使用协程(但如何正确地设计它们以使其正常工作?)

  • 使用某些东西来检测文件是否被写入或写入锁是否打开(我该如何做到这一点?)

目标是我通过copas(http://keplerproject.github.io/copas/)接收要ping不同IP并报告IP是否存活的“查找”请求。 Copas已经运行,到目前为止工作正常。

感谢您的所有帮助。

点赞
用户1993231
用户1993231

io.popen 不适用于 Copas。Copas 是用于 sockets 而不是 pipes 的。

最快的解决方案是使用轮询; 例如:

local socket = require "socket"
local waitTime, sleepTime = 5, 0.1
local endT = os.time() + waitTime
local result
os.capture("ping myknowngoodip -c 1 > myoutfile")
while os.time() <= endT do
    local pcall_ok, fHand = pcall(io.open, "myoutfile", "r")
    if pcall_ok then
        result = assert(fHand:read("*all"))
        assert(fHand:close())
    end
    socket.sleep(sleepTime)
end
if result then
    -- Success!
else
    -- Error :(
end

另一个可能性是使用单独的线程; LuaLanes 是最好的多线程工具包,但在内存消耗方面也是最大的。这有点复杂。

另一个选项是使用 Copas 或 LOOP 的 SocketScheduler 实现自己的 ping(建议使用后者,它比 Copas 好得多);这将是异步的。LOOP.SocketScheduler 也是一个很棒的协程管理框架,适用于各种事情,并且可以与上面示例的代码一起使用(用 scheduler:sleep 替换 socket.sleep,并在新创建并注册的协程中运行代码)。

如果你是熟练的 C 程序员,你也可以让 C 来完成工作;例如调用一个 C 函数,传递一个 Lua 回调函数,并在 ping 完成后从新创建的 pthread 中调用回调。

免责声明:代码未经测试。

2013-06-27 09:16:06