Class: Asynchronous::Parallelism

Inherits:
CleanClass show all
Defined in:
lib/asynchronous/parallelism.rb

Overview

now let’s see the Parallelism you can use simple :p also instead of :parallelism remember :parallelism is all about real OS thread case, so you CANT modify the objects in memory only copy on write modify This is ideal for big operations where you need do a big process and only get the return value so you can do big works without the fear of the Garbage collector slowness or the GIL lock when you need to update objects in the memory use :concurrency

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(callable) ⇒ Parallelism

Returns a new instance of Parallelism.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/asynchronous/parallelism.rb', line 57

def initialize callable

  @comm_line   = ::IO.pipe
  @value       = nil
  @read_buffer = nil

  asynchronous_read_buffer
  @pid= asynchronous_fork callable
  @@pids.push(@pid)

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/asynchronous/parallelism.rb', line 119

def method_missing(method, *args)

  return_value= nil
  new_value= asynchronous_get_value
  begin
    original_value= new_value.dup
  rescue ::TypeError
    original_value= new_value
  end
  return_value= new_value.__send__(method,*args)
  unless new_value == original_value
      asynchronous_set_value new_value
    end

  return return_value

end

Class Method Details

.asynchronous_alive?(pid) ⇒ Boolean

Returns:



73
74
75
76
77
78
79
80
# File 'lib/asynchronous/parallelism.rb', line 73

def self.asynchronous_alive?(pid)
  begin
    ::Process.kill(0,pid)
    return true
    rescue ::Errno::ESRCH
    return false
  end
end

Instance Method Details

#asynchronous_fork(callable) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/asynchronous/parallelism.rb', line 15

def asynchronous_fork callable
  return ::Kernel.fork do

    begin
      ::Kernel.trap("TERM") do
        ::Kernel.exit
      end
      ::Thread.new do
        ::Kernel.loop do
          begin
            ::Kernel.sleep 1
            if ::Asynchronous::Parallelism.asynchronous_alive?(@@motherpid) == false
              ::Kernel.exit
            end
          end
        end
      end
    end

    @comm_line[0].close
    @comm_line[1].write ::Marshal.dump(callable.call)
    @comm_line[1].flush

    #::Kernel.loop do
    #  #::Kernel.puts @comm_line[0].closed?
    #  #::Kernel.puts @comm_line[1].closed?
    #  ::Kernel.sleep 1
    #end


  end
end

#asynchronous_get_pidObject



69
70
71
# File 'lib/asynchronous/parallelism.rb', line 69

def asynchronous_get_pid
  return @pid
end

#asynchronous_get_valueObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/asynchronous/parallelism.rb', line 82

def asynchronous_get_value

  if @value.nil?

    ::Process.wait(@pid, ::Process::WNOHANG )

    @comm_line[1].close
    @read_buffer.join
    @comm_line[0].close

  end

  return @value

end

#asynchronous_read_bufferObject



48
49
50
51
52
53
54
# File 'lib/asynchronous/parallelism.rb', line 48

def asynchronous_read_buffer
  @read_buffer = ::Thread.new do
    while !@comm_line[0].eof?
      @value = ::Marshal.load(@comm_line[0])
    end
  end
end

#asynchronous_set_value(obj) ⇒ Object Also known as: asynchronous_set_value=



98
99
100
# File 'lib/asynchronous/parallelism.rb', line 98

def asynchronous_set_value(obj)
  @value= obj
end

#synchronizeObject Also known as: sync



103
104
105
# File 'lib/asynchronous/parallelism.rb', line 103

def synchronize
  asynchronous_get_value
end