【问题标题】:Lua fork concurrent processesLua fork 并发进程
【发布时间】:2012-11-05 08:10:14
【问题描述】:

我想从一个 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 detected free(): invalid next size (fast): 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

【问题讨论】:

  • "我无法通过 posix.fork() 找到任何示例。" 你能不能简单地尝试一下,看看会发生什么?
  • 但我真的不知道它的用法。好的,通过在网上搜索东西并阅读代码进行了大量的恶化,但没有运气

标签: lua fork


【解决方案1】:

posix.fork() 是luaposix library 的一部分,可以通过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

【讨论】:

    【解决方案2】:

    您可能想尝试Lua Lanes(或来自here),它是Lua 的可移植线程库。

    【讨论】:

    • here 是 Lua 其他并发库的比较
    • here 是通道的示例(适用于 LuaForWindows 中的通道)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 2011-06-27
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    • 2013-02-28
    相关资源
    最近更新 更多