等待popen3过程完成?
问题描述:
我有一个Rails应用程序,使用Open3.popen3
。它工作正常,但有时应用程序将继续而不等待该过程完成。等待popen3过程完成?
这是我正在使用看起来像Open3.popen3
功能(实际上它运行cat
功能):
def cat_func(var)
## some stuff happens
exit = 0
Open3.popen3(" #{cat_command}"){|stdin, stdout, stderr, wait_thr|
pid = wait_thr.pid
error = std err.gets
exit = wait_thr.value
}
#HERE IS TRYING TO INTERCEPT ERRORS:
if error.match(/^cat:/)
### Do something
end
call_next_function
end
我在做什么错?
答
只是一个猜测:也许你也必须消耗标准输出,所以也许像添加
def cat_func(var)
exit = 0
Open3.popen3(" #{cat_command}") do |stdin, stdout, stderr, wait_thr|
pid = wait_thr.pid
stdout.gets
error = stderr.gets
exit = wait_thr.value
end
# more stuff...
end
修复该问题的行?
答
为了记录在案,这里是我的最终解决方案:
Open3.popen3(command) do |stdin, stdout, stderr|
stdin.puts instructions
stdin.close # make sure the subprocess is done
stdout.gets # and read all output - EOF means the process has completed.
stderr.gets
end
当它的工作原理,在不工作? – Linuxios 2013-03-20 00:52:22