Linux PTY 上的 Ruby 在没有 EOF 的情况下消失,引发 Errno::EIO [英] Ruby on Linux PTY goes away without EOF, raises Errno::EIO

查看:21
本文介绍了Linux PTY 上的 Ruby 在没有 EOF 的情况下消失,引发 Errno::EIO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些代码,它获取一个文件,将该文件传递给几个二进制文件之一进行处理,并监视转换过程中的错误.我已经在 OSX 上编写并测试了以下例程,但由于我不清楚的原因,linux 失败了.

I'm writing some code which takes a file, passes that file to one of several binaries for processing, and monitors the conversion process for errors. I've written and tested the following routine on OSX but linux fails for reasons about which I'm not clear.

#run the command, capture the output so it doesn't display
PTY.spawn(command) {|r,w,pid|
    until r.eof? do
      ##mark
      puts r.readline
    end
}

运行的命令变化很大,##mark 处的代码已被简化为本地回显,以尝试调试问题.命令执行,脚本在终端打印预期的输出,然后抛出异常.

The command that runs varies quite a lot and the code at the ##mark has been simplified into a local echo in an attempt to debug the problem. The command executes and the script prints the expected output in the terminal and then throws an exception.

它在 Debian 系统上产生的错误是:Errno::EIO (Input/output error -/dev/pts/0):

The error it produces on Debian systems is: Errno::EIO (Input/output error - /dev/pts/0):

我能想到的所有命令字符串都会产生该错误,当我在没有本地 echo 块的情况下运行代码时,它运行得很好:

All of the command strings I can come up with produce that error, and when I run the code without the local echo block it runs just fine:

PTY.spawn(command) {|r,w,pid|}

在任何一种情况下,命令本身都可以正常执行,但似乎 debian linux 没有将 eof 发送到 pty.PTY 的文档页面和 ruby​​-doc 上的 IO 似乎在这里没有任何帮助.

In either case the command itself executes fine, but it seems like debian linux isn't sending eof up the pty. The doc pages for PTY, and IO on ruby-doc don't seem to lend any aid here.

有什么建议吗?谢谢.

-vox-

推荐答案

所以我不得不去阅读 PTY 库的 C 源代码才能真正对这里发生的事情感到满意.

So I had to go as far as reading the C source for the PTY library to get really satisfied with what is going on here.

Ruby PTY 文档并没有真正说明评论的内容 在源代码中说.

The Ruby PTY doc doesn't really say what the comments in the source code say.

我的解决方案是组合一个包装器方法,并在需要时从我的脚本中调用它.我还加入了等待进程以确保退出并从 $? 访问退出状态的方法:

My solution was to put together a wrapper method and to call that from my script where needed. I've also boxed into the method waiting on the process to for sure exit and the accessing of the exit status from $?:

# file: lib/safe_pty.rb

require 'pty'
module SafePty
  def self.spawn command, &block

    PTY.spawn(command) do |r,w,p|
      begin
        yield r,w,p
      rescue Errno::EIO
      ensure
        Process.wait p
      end
    end

    $?.exitstatus
  end
end

这个用法和PTY.spawn基本一样:

This is used basically the same as PTY.spawn:

require 'safe_pty'
exit_status = SafePty.spawn(command) do |r,w,pid|
  until r.eof? do
    logger.debug r.readline
  end
end

#test exit_status for zeroness

当我发现这是一个有效的响应时,我感到非常沮丧,因为它在 ruby​​-doc 上完全没有记录.

I was more than a little frustrated to find out that this is a valid response, as it was completely undocumented on ruby-doc.

这篇关于Linux PTY 上的 Ruby 在没有 EOF 的情况下消失,引发 Errno::EIO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆