接收分叉进程输出外部方法



我是ruby和面向对象语言的新手,我很难找到一种方法来完成在方法内部分叉进程并传递延迟输出以在方法外部使用,同时返回进程ID。

def method(arg)
    proc_id = fork do
        var = `command #{arg}`
    end
    return both = [proc_id, var]
end

这不起作用,因为var将返回nil因为该过程尚未完成。 我怎么能完成这样的事情?

更新:

使用IO.pipe我能够完成进程间通信。 但是,尝试在方法中使用此解决方案将不允许我在不首先等待该过程完成的情况下同时返回proc_idvar,这迫使我创建新的数组和迭代,否则这是不必要的。 此处的目标是在方法内部的fork进程仍在工作时,可以自由地在方法外部执行代码。

arg_array = ["arg1", "arg2", "arg3", "arg4"]
input = []
output = []
proc_id = []
arg_array.each_index do |i|
    input[i], output[i] = IO.pipe
    proc_id[i] = fork do
        input[i].close
        output[i].write `command #{arg_array[i]}`
    end
    output[i].close
end
command2
command3
include Process
waitpid(proc_id[0])
command4
Process.waitall
arg_array.each_index do |x|
    puts input[x].read
end

你需要花更多的时间来研究fork的概念。分叉后的父进程和子进程在不使用IPC(进程间通信)的情况下无法相互通信(交换变量),这有点复杂。

但出于您的目的(获取子进程 ID 及其输出),使用 Open3.popen2 或 Open3.popen3 会更容易。

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open3/rdoc/Open3.html#method-c-popen2

如果你想启动一些东西并保存子PID,那相当简单。

pid = fork
if pid
    return pid
else
    system("command #{arg}")
    exit
end

有点笨拙,但基本上,fork将子 PID 返回到父进程,nil返回到子进程。 确保你退出孩子,它不会自动这样做。

感谢 jaeheung 的建议,我已经使用 Open3.popen2 解决了这个问题(需要 1.9.3 版)。

arguments = ["arg1", "arg2", "arg3", "arg4"]
require 'open3'
include Open3
def method(arg)
    input, output, thread = Open3.popen2("command #{arg}")
    input.close
    return [thread.pid, output]
end
thread_output = []
arguments.each do |i|
    thread_output << method("#{i}")
end
command1
command2
include Process
waitpid(thread_output[0][0])
command3
Process.waitall
thread_output.each do |x|
    puts x[1].read
end

最新更新