Sha256: 689b4b9adc72fe5e0e9613a99b4091bc94154926c3dac59cc04590d4a43cfe3a

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

module Asynchronous
  # 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 Parallelism < CleanClass

    # Basic
    begin

      #require 'yaml'
      @@pids=[]
      def initialize callable

        @value= nil
        @rd, @wr = ::IO.pipe
        @pid= ::Kernel.fork do

          ::Kernel.trap("TERM") do
            exit
          end

          @rd.close
          @wr.write ::Marshal.dump(callable.call)#.to_yaml
          @wr.close

        end

        @@pids.push(@pid)

      end

    end

    # return value
    begin

      def value

        if @value.nil?

          @wr.close
          return_value= @rd.read
          return_value= ::Marshal.load(return_value)
          @rd.close
          @@pids.delete(@pid)
          @value= return_value

        end

        return @value

      end


      def value=(obj)
        @value= obj
      end

      #def method_missing(method,*args)
      #  value.__send__(method,*args)
      #end
      #
      #def respond_to_missing?(method, include_private = false)
      #  value.respond_to?(method, include_private)
      #end

    end

    # kill Zombies at Kernel Exit
    begin
      ::Kernel.at_exit {
        @@pids.each { |pid|
          begin
            ::Process.kill(:TERM, pid)
          rescue ::Errno::ESRCH, ::Errno::ECHILD
            ::STDOUT.puts "`kill': No such process (Errno::ESRCH)"
          end
        }
      }
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
asynchronous-0.1.0 lib/asynchronous/parallelism.rb