Не удалось загрузить DLL-файл

Возможно, это поможет кому-то еще искать аналогичные функции тайм-аута, но ему нужно собрать результат из команды оболочки.

Я адаптировал метод @ shurikk для работы с Ruby 2.0 и некоторый код из Выполняет дочерний процесс Fork с тайм-аутом и выходом захвата для сбора выходных данных.

def exec_with_timeout(cmd, timeout)
  begin
    # stdout, stderr pipes
    rout, wout = IO.pipe
    rerr, werr = IO.pipe
    stdout, stderr = nil

    pid = Process.spawn(cmd, pgroup: true, :out => wout, :err => werr)

    Timeout.timeout(timeout) do
      Process.waitpid(pid)

      # close write ends so we can read from them
      wout.close
      werr.close

      stdout = rout.readlines.join
      stderr = rerr.readlines.join
    end

  rescue Timeout::Error
    Process.kill(-9, pid)
    Process.detach(pid)
  ensure
    wout.close unless wout.closed?
    werr.close unless werr.closed?
    # dispose the read ends of the pipes
    rout.close
    rerr.close
  end
  stdout
 end

0
задан Bill 5 May 2019 в 18:16
поделиться