class Fibril::NonBlockingIOWrapper

Attributes

fibrils[RW]

A Non block IO wrapper allows you to execute a blocking IO loop inside a separate thread and receive all inputs inside one or more Fibrils.

This allows you to have multiple block IO loops operating in parallel whilst still processing all resulting messages in the main thread.

guard[RW]

A Non block IO wrapper allows you to execute a blocking IO loop inside a separate thread and receive all inputs inside one or more Fibrils.

This allows you to have multiple block IO loops operating in parallel whilst still processing all resulting messages in the main thread.

response_queue[RW]

A Non block IO wrapper allows you to execute a blocking IO loop inside a separate thread and receive all inputs inside one or more Fibrils.

This allows you to have multiple block IO loops operating in parallel whilst still processing all resulting messages in the main thread.

result[RW]

A Non block IO wrapper allows you to execute a blocking IO loop inside a separate thread and receive all inputs inside one or more Fibrils.

This allows you to have multiple block IO loops operating in parallel whilst still processing all resulting messages in the main thread.

Public Class Methods

new(*, &block) click to toggle source
# File lib/fibril/non_blocking_io_wrapper.rb, line 11
def initialize(*, &block)
  self.response_queue = []
  self.fibrils = []
  define_singleton_method(:loop, &block)
  future{
    begin
      loop()
    rescue Exception => e
      puts "Exception occurred in thead #{Thread.current} : #{e.message}"
      puts e.backtrace
    end
  }
end

Public Instance Methods

await() click to toggle source
# File lib/fibril/non_blocking_io_wrapper.rb, line 50
def await
  ##
  # Set all fibrils into waiting state until there is something in the response queue
  ##
  Fibril.current.yield{|f| self.fibrils << f} until self.response_queue.any?
  ##
  # Return values from the response queue
  ##
  self.response_queue.shift
end
ingest(*args) click to toggle source

Add the ingested message to the response queue and schedule all fibrils waiting on events to receive messages

# File lib/fibril/non_blocking_io_wrapper.rb, line 40
def ingest(*args)
  begin
    self.response_queue << args
    self.fibrils.shift.enqueue while self.fibrils.any?
  rescue Exception => e
    puts "Exception occurred when ingesting from #{self} : #{e.message}"
    puts e.backtrace
  end
end
receive(*args) click to toggle source

Receive a message from the async IO loop if a message is sent, otherwise return a reference to the ingest method

# File lib/fibril/non_blocking_io_wrapper.rb, line 28
def receive(*args)
  if args.any?
    ingest(*args)
  else
    method(:ingest)
  end
end