当我尝试使用“socket”库中的“read_nonblock”方法时,出现以下错误
IO::EAGAINWaitReadable: Resource temporarily unavailable - read would block
但是当我通过终端上的 IRB 尝试它时,它工作正常
我怎样才能让它读取缓冲区?
当我尝试使用“socket”库中的“read_nonblock”方法时,出现以下错误
当缓冲区中的数据未准备好时,这是预期的行为。自从出现异常之后,IO::EAGAINWaitReadable
源自ruby版本2.1.0
,在旧版本中你必须捕获IO::WaitReadable
带有额外的端口选择和重试。因此,请按照以下建议进行操作红宝石文档 http://ruby-doc.org/core-2.3.0/IO.html#method-i-read_nonblock:
begin
result = io.read_nonblock(maxlen)
rescue IO::WaitReadable
IO.select([io])
retry
end
对于较新版本的 os ruby 你应该 trapIO::EAGAINWaitReadable
也可以,但只是超时或无限重试阅读。我还没有在文档中找到该示例,但请记住它没有端口选择:
begin
result = io.read_nonblock(maxlen)
rescue IO::EAGAINWaitReadable
retry
end
然而,我的一些调查表明,最好在IO::EAGAINWaitReadable
,这样你就可以得到:
begin
result = io.read_nonblock(maxlen)
rescue IO::WaitReadable, IO::EAGAINWaitReadable
IO.select([io])
retry
end
要支持具有两个版本的异常的代码,只需声明以下定义IO::EAGAINWaitReadable
in lib/核心下if
条款:
if ! ::IO.const_defined?(:EAGAINWaitReadable)
class ::IO::EAGAINWaitReadable; end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)