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?
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 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
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 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With