IO::EAGAINWaitReadable: Resource temporarily unava

2019-04-27 17:56发布

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

But when I try it through the IRB on the terminal it works fine

How can I make it read the buffer?

1条回答
一夜七次
2楼-- · 2019-04-27 18:20

I get the following error when I try to use the method "read_nonblock" from the "socket" library

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

For newer version os ruby you should trap IO::EAGAINWaitReadable also, but just with retrial reading for a timeout or infinitely. I haven't find 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

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

To support the code woth 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
查看更多
登录 后发表回答