当我尝试使用“socket”库中的“read_nonblock”方法时,出现以下错误。
IO::EAGAINWaitReadable: Resource temporarily unavailable - read would block
但是当我在终端上通过IRB尝试时,它可以正常工作。
我该如何让它读取缓冲区?
IO::EAGAINWaitReadable: Resource temporarily unavailable - read would block
但是当我在终端上通过IRB尝试时,它可以正常工作。
我该如何让它读取缓冲区?
begin
result = io.read_nonblock(maxlen)
rescue IO::WaitReadable
IO.select([io])
retry
end
对于更新版的 Ruby,您需要捕获 IO::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
if
子句中声明IO :: EAGAINWaitReadable
的定义:if ! ::IO.const_defined?(:EAGAINWaitReadable)
class ::IO::EAGAINWaitReadable; end
end