Class: Fibril::FAsyncProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/fibril/fasync_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ FAsyncProxy

Returns a new instance of FAsyncProxy.



6
7
8
# File 'lib/fibril/fasync_proxy.rb', line 6

def initialize(target)
  self.target = target
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *_args, &_block) ⇒ Object

Execute target method within a new fork. Enqueue the current fibril to be resumed as soon as async task is finished. The result of the forked process is passed to the parent by Marshaling.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fibril/fasync_proxy.rb', line 15

def method_missing(name, *_args, &_block)
  define_singleton_method(name){|*args, &block|
    read, write = IO.pipe
    waiting = Fibril.current
    pid = fork do
      result = target.send(name, *args, &block)
      read.close
      YAML.dump(result, write)
    end
    write.close
    result = nil
    Thread.new{
      result = read.read
      Process.wait(pid)
      Fibril.enqueue waiting
    }
    Fibril.current.yield
    YAML.load(result)
  }
  send(name, *_args, &_block)
end

Instance Attribute Details

#targetObject

Returns the value of attribute target.



4
5
6
# File 'lib/fibril/fasync_proxy.rb', line 4

def target
  @target
end