我有一个使用Open3.popen3的Rails应用程序。它工作得很好,但有时应用程序会继续运行,而不等待进程完成。
下面是我在中使用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我做错了什么?
发布于 2013-03-20 17:48:13
仅供猜测:也许您还必须使用stdout,因此可能需要添加一行代码,例如
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解决了这个问题?
发布于 2013-03-21 23:45:25
郑重声明,这是我的最终解决方案:
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
endhttps://stackoverflow.com/questions/11710542
复制相似问题