gpt4 book ai didi

ruby-on-rails - 等待 popen3 进程完成?

转载 作者:太空宇宙 更新时间:2023-11-03 16:07:04 25 4
gpt4 key购买 nike

我有一个使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com