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

标签 ruby-on-rails ruby process popen

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

相关文章:

ruby-on-rails - 翻译设计登录链接

ruby-on-rails - Expire选项在Rails.cache中不起作用

ruby-on-rails - 在 RSpec 中,在 :all block 之前使用 let 变量

ruby - 错误信息 : -bash:/etc/profile. d/rvm.sh: 没有那个文件或目录

ruby-on-rails - 在远程服务器上执行 rake 任务

ruby - 绑定(bind)到 ruby​​ 中的网络接口(interface)

c# - 无法启动多个进程 (C#)

ruby-on-rails - Rails - PG::数据类型不匹配:错误

c# - 具有多个参数的 Shutdown.exe 进程,不工作

linux - 我应该如何在后台运行我的 Golang 进程?