Method: IO#await_readable

Defined in:
lib/all/io.rb

#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.

Examples:

Waiting inside a concurrent evaluation

# Control flow is indicated by (N)

r,w = IO.pipe

# (1)
reader = concurrently do
  # (4)
  r.await_readable
  # (6)
  r.read
end

# (2)
concurrently do
  # (5)
  w.write 'Hey from the other proc!'
  w.close
end

# (3)
reader.await_result # => 'Hey from the other proc!'
# (7)

r.close

Waiting outside a concurrent evaluation

# Control flow is indicated by (N)

r,w = IO.pipe

# (1)
concurrently do
  # (3)
  puts "I'm running while the outside is waiting!"
  w.write "Continue!"
  w.close
end

# (2)
r.await_readable
# (4)
r.read # => "Continue!"

r.close

Waiting with a timeout

r,w = IO.pipe
r.await_readable(within: 1)
# => raises a TimeoutError after 1 second

Waiting with a timeout and a timeout result

r,w = IO.pipe
r.await_readable(within: 0.1, timeout_result: false)
# => returns false after 0.1 seconds

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :within (Numeric)

    maximum time to wait (defaults to: Float::INFINITY)

  • :timeout_result (Object)

    result to return in case of an exceeded waiting time (defaults to raising Concurrently::Evaluation::TimeoutError)

Returns:

  • (true)

Raises:

Since:

  • 1.0.0



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