IO::EAGAINWaitReadable: 资源暂时不可用 - 读取会阻塞 [英] IO::EAGAINWaitReadable: Resource temporarily unavailable - read would block
问题描述
当我尝试使用socket"库中的read_nonblock"方法时出现以下错误
I get the following error when I try to use the method "read_nonblock" from the "socket" library
IO::EAGAINWaitReadable: Resource temporarily unavailable - read would block
但是当我通过终端上的 IRB 尝试它时,它工作正常
But when I try it through the IRB on the terminal it works fine
如何让它读取缓冲区?
推荐答案
当我尝试使用方法read_nonblock"时出现以下错误;来自插座"图书馆
I get the following error when I try to use the method "read_nonblock" from the "socket" library
当缓冲区中的数据未准备好时,这是预期的行为.由于异常,IO::EAGAINWaitReadable
源自 ruby 版本 2.1.0
,在旧版本中,您必须使用附加的陷阱 IO::WaitReadable
端口选择并重试.因此,请按照 ruby 文档 中的建议进行操作一个>:
It is expected behaviour when the data isn't ready in the buffer. Since the exception, IO::EAGAINWaitReadable
is originated from ruby version 2.1.0
, in older version you must trap IO::WaitReadable
with additional port selection and retry. So do as it was adviced in the ruby documentation:
begin
result = io.read_nonblock(maxlen)
rescue IO::WaitReadable
IO.select([io])
retry
end
对于较新版本的 os ruby,您也应该捕获 IO::EAGAINWaitReadable
,但只是重试读取超时或无限.我还没有在文档中找到示例,但请记住它没有选择端口:
For newer version os ruby you should trap IO::EAGAINWaitReadable
also, but just with retrial reading for a timeout or infinitely. I haven't found out the example in the docs, but remember that it was without port selection:
begin
result = io.read_nonblock(maxlen)
rescue IO::EAGAINWaitReadable
retry
end
但是我的一些调查导致最好在IO::EAGAINWaitReadable
上进行端口选择,这样你就可以得到:
However some my investigations lead to that it is also better to do port selection on IO::EAGAINWaitReadable
, so you'll can get:
begin
result = io.read_nonblock(maxlen)
rescue IO::WaitReadable, IO::EAGAINWaitReadable
IO.select([io])
retry
end
要支持两种版本的异常代码,只需在lib/核心的if
子句下声明IO::EAGAINWaitReadable
的定义:
To support the code with both versions of exception, just declare the definition of IO::EAGAINWaitReadable
in lib/ core under if
clause:
if ! ::IO.const_defined?(:EAGAINWaitReadable)
class ::IO::EAGAINWaitReadable; end
end
这篇关于IO::EAGAINWaitReadable: 资源暂时不可用 - 读取会阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!