我有一个使用 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
我做错了什么?
最佳答案
只是一个猜测:也许你还必须使用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
问题解决了吗?
关于ruby-on-rails - 等待 popen3 进程完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11710542/