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_readable(opts = {}) ⇒ true
Suspends the current evaluation until IO is readable.
-
#await_writable(opts = {}) ⇒ true
Suspends the current evaluation until IO is writable.
-
#concurrently_read(maxlen, outbuf = nil) ⇒ Object
Reads from IO concurrently.
-
#concurrently_write(string) ⇒ Integer
Writes to IO concurrently.
Instance Method Details
#await_readable(opts = {}) ⇒ true
Suspends the current evaluation until IO is readable. It can be used inside and outside of concurrent procs.
While waiting, the code jumps to the event loop and executes other concurrent procs 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. It can be used inside and outside of concurrent procs.
While waiting, the code jumps to the event loop and executes other concurrent procs that are ready to run in the meantime.
208 209 210 211 212 213 214 |
# File 'lib/all/io.rb', line 208 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 |
#concurrently_read(maxlen) ⇒ String #concurrently_read(maxlen, outbuf) ⇒ outbuf
Reads from IO concurrently.
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
read_nonblock(maxlen, buf)
rescue IO::WaitReadable
await_readable
retry
end
121 122 123 124 125 126 |
# File 'lib/all/io.rb', line 121 def concurrently_read(maxlen, outbuf = nil) read_nonblock(maxlen, outbuf) rescue IO::WaitReadable await_readable retry end |
#concurrently_write(string) ⇒ Integer
Writes to IO concurrently.
If IO is not writable right now it blocks the current concurrent proc and tries again after it became writable.
This methods is a shortcut for:
begin
write_nonblock(string)
rescue IO::WaitWritable
await_writable
retry
end
242 243 244 245 246 247 |
# File 'lib/all/io.rb', line 242 def concurrently_write(string) write_nonblock(string) rescue IO::WaitWritable await_writable retry end |