4

我想从 lua 脚本同时执行后台进程

喜欢 :

a = io.popen("deploy.exp" .. ip1):read("*a")
b = io.popen("deploy.exp" .. ip2):read("*a")

其中 a,b 是不断运行的进程。当我按上述方式执行此操作时,b 只会在 a 完成时运行。deploy.exp 脚本是一个expect 脚本,用于ssh 一些服务器,并执行一些命令。然后我需要从 a 和 b 获取一些文本。对此有任何想法吗?我尝试了 ExtensionProposal API。当我尝试时,我收到一条错误消息,上面写着:“ * glibc detectedfree():下一个大小无效(快速):0x08aa2300 * * abort"。

部分代码是

for k,v in pairs(single) do
command =  k .. " 1 " ..  table.concat(v, " ")
local out = io.pipe()
local pro = assert(os.spawn("./spaw.exp " .. command,{
      stdout = out,  
}))
if not proc then error("Failed to aprogrinate! "..tostring(err)) end
print(string.rep("#", 50))
local exitcode = proc:wait()
end

有没有人对此有任何经验(或建议/我们应该在哪里看)?或者给我一个样品?谢谢

顺便说一句:我尝试了 luaposix,但我无法通过 posix.fork() 找到任何示例。有没有人可以分享一个?TKS

4

2 回答 2

3

posix.fork() 是luaposix 库的一部分,可以通过luarocks安装。它的工作方式与fork(3)大致相同;它创建父进程的副本,并且它们都将在调用 fork() 之后执行所有操作。fork() 的返回值在子进程中为 0,否则为刚刚生成的子进程的 PID。这是一个人为的例子:

local posix = require "posix"
local pid = posix.fork()

if pid == 0 then 
  -- this is the child process
  print(posix.getpid('pid') .. ": child process")

else 
  -- this is the parent process
  print(posix.getpid('pid') .. ": parent process")

  -- wait for the child process to finish
  posix.wait(pid) 

end

-- both processes get here
print(posix.getpid('pid') .. ": quitting")

这应该输出如下内容:

$ lua fork.lua 
27219: parent process
27220: child process
27220: quitting
27219: quitting
于 2012-11-07T19:14:09.530 回答
1

您可能想尝试Lua Lanes(或从这里),它是 Lua 的可移植线程库。

于 2012-11-05T09:14:14.013 回答