Class: IO
- Inherits:
-
Object
- Object
- IO
- Defined in:
- lib/all/io.rb
Overview
Concurrently adds a few methods to IO
which make them available
for every IO instance.
Instance Method Summary collapse
-
#await_read(maxlen, outbuf = nil) ⇒ Object
Waits until successfully read from IO with blocking other evaluations.
-
#await_readable(opts = {}) ⇒ true
Suspends the current evaluation until IO is readable.
-
#await_writable(opts = {}) ⇒ true
Suspends the current evaluation until IO is writable.
-
#await_written(string) ⇒ Integer
Waits until successfully written to IO with blocking other evaluations.
-
#concurrently_read(maxlen, outbuf = nil) ⇒ Object
Reads from IO concurrently.
-
#concurrently_write(string) ⇒ Integer
Writes to IO concurrently.
Instance Method Details
#await_read(maxlen) ⇒ String #await_read(maxlen, outbuf) ⇒ outbuf
Waits until successfully read from IO with blocking other evaluations.
If IO is not readable right now it blocks the current concurrent evaluation and tries again after it became readable.
This method is a shortcut for:
begin
io.read_nonblock(maxlen, outbuf)
rescue IO::WaitReadable
io.await_readable
retry
end
123 124 125 126 127 128 |
# File 'lib/all/io.rb', line 123 def await_read(maxlen, outbuf = nil) read_nonblock(maxlen, outbuf) rescue IO::WaitReadable await_readable retry end |
#await_readable(opts = {}) ⇒ true
Suspends the current evaluation until IO is readable.
While waiting, the code jumps to the event loop and executes other evaluations that are ready to run in the meantime.
77 78 79 80 81 82 83 |
# File 'lib/all/io.rb', line 77 def await_readable(opts = {}) io_selector = Concurrently::EventLoop.current.io_selector io_selector.await_reader(self, Concurrently::Evaluation.current) await_resume! opts ensure io_selector.cancel_reader(self) end |
#await_writable(opts = {}) ⇒ true
Suspends the current evaluation until IO is writable.
While waiting, the code jumps to the event loop and executes other evaluations that are ready to run in the meantime.
252 253 254 255 256 257 258 |
# File 'lib/all/io.rb', line 252 def await_writable(opts = {}) io_selector = Concurrently::EventLoop.current.io_selector io_selector.await_writer(self, Concurrently::Evaluation.current) await_resume! opts ensure io_selector.cancel_writer(self) end |
#await_written(string) ⇒ Integer
Waits until successfully written to IO with blocking other evaluations.
If IO is not writable right now it blocks the current evaluation and tries again after it became writable.
This methods is a shortcut for:
begin
io.write_nonblock(string)
rescue IO::WaitWritable
io.await_writable
retry
end
288 289 290 291 292 293 |
# File 'lib/all/io.rb', line 288 def await_written(string) write_nonblock(string) rescue IO::WaitWritable await_writable retry end |
#concurrently_read(maxlen) ⇒ String #concurrently_read(maxlen, outbuf) ⇒ outbuf
Reads from IO concurrently.
Reading is done in a concurrent evaluation in the background.
This method is a shortcut for:
concurrently{ io.await_read(maxlen, outbuf) }
162 163 164 |
# File 'lib/all/io.rb', line 162 def concurrently_read(maxlen, outbuf = nil) READ_PROC.call_detached(self, maxlen, outbuf) end |
#concurrently_write(string) ⇒ Integer
Writes to IO concurrently.
Writing is done in a concurrent evaluation in the background.
This method is a shortcut for:
concurrently{ io.await_written(string) }
317 318 319 |
# File 'lib/all/io.rb', line 317 def concurrently_write(string) WRITE_PROC.call_detached(self, string) end |