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

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

问题描述

我正在写一些接受文件的代码,将该文件传递给几个二进制文件之一进行处理,并监视转换过程的错误。我已经在OSX上编写和测试了以下例程,但是由于我不清楚的原因,linux出现故障。

  #run该命令捕获输出,因此不显示
PTY.spawn(command){| r,w,pid |
,直到r.eof? do
## mark
puts r.readline
end
}

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



在Debian系统上产生的错误是: Errno :: EIO(输入/输出错误 - / dev / pts / 0):



我可以想出所有的命令字符串那个错误,当我运行没有本地回声块的代码,它运行正常:

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

在任一情况下,命令本身执行正常,但似乎debian linux没有发送电子邮件。 PTY的文档页面和ruby-doc上的IO似乎没有任何帮助。



任何建议?谢谢。



-vox -

解决方案

所以我不得不去只要阅读PTY库的C源,就可以对这里发生的事情感到非常满意。



Ruby PTY文档并没有真正说明什么意见<源代码中的href =https://github.com/ruby/ruby/blob/57fb2199059cb55b632d093c2e64c8a3c60acfbb/ext/pty/pty.c#L519 =noreferrer>说:



我的解决方案是将一个包装方法放在一起,并从需要的脚本中调用它。我也在等待进程的方法中确定退出并从 $?访问退出状态:

 #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
确保
Process.wait p
end
end

$ ?. exitstatus
end
end

这是使用基本上与PTY.spawn相同:

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

#test exit_status for zeroness

我发现这是一个有效的回应,因为它完全没有文件在ruby-doc上,我感到有点沮丧。


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
}

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.

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

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|}

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.

Any suggestions? Thanks.

-vox-

解决方案

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.

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

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

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

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

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