ruby LolConcurrency :: Actor制作自己的赛璐珞

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby LolConcurrency :: Actor制作自己的赛璐珞相关的知识,希望对你有一定的参考价值。

require 'forwardable'
require 'thread'
require 'monitor'

module LolConcurrency
  module Future
    Future = Struct.new(:queue) do
      include MonitorMixin

      private :queue

      def value
        @value ||= begin
          synchronize do
            @value ||= queue.pop
          end
        end
      end
    end

    Factory = Struct.new(:instance) do
      extend Forwardable
      def_delegator :instance, :respond_to?

      private :instance

      def method_missing(method, *args, &block)
        queue = SizedQueue.new(1)
        Thread.new do
          queue << instance.public_send(method, *args, &block)
        end
        Future.new(queue)
      end
    end

    def future
      @future ||= begin
        synchronize do
          @future ||= Factory.new(self)
        end
      end
    end

    def self.included(klass)
      unless klass < MonitorMixin
        klass.send(:include, MonitorMixin)
      end
    end
  end
end
require 'forwardable'
require 'thread'
require 'monitor'

module LolConcurrency
  module Actor
    Context = Struct.new(:method, :args, :block)

    Async = Struct.new(:instance, :mailbox) do
      extend Forwardable
      def_delegator :instance, :respond_to?

      private :instance
      private :mailbox

      def initialize(instance)
        super(instance, Queue.new)
        run!
      end

      def method_missing(method, *args, &block)
        mailbox << Context.new(method, args, block)
        nil
      end

    private

      def run!
        Thread.new do
          loop do
            ctx = mailbox.pop
            instance.public_send(ctx.method, *ctx.args, &ctx.block)
          end
        end
      end
    end

    def async
      @async ||= begin
        synchronize do
          @async ||= Async.new(self)
        end
      end
    end

    def self.included(klass)
      unless klass < MonitorMixin
        klass.send(:include, MonitorMixin)
      end
    end
  end
end

以上是关于ruby LolConcurrency :: Actor制作自己的赛璐珞的主要内容,如果未能解决你的问题,请参考以下文章

ruby 正则表达式Regexp

ruby factory_girlで自定义策略を作る提示参考:http://qiita.com/kitaro_tn/items/6200cbabbbea0c1ac168

ruby RailsのAPIをswaggerでドキュメント化してみるref:http://qiita.com/kitaro_tn/items/ace80a36ac3cfc060788

Ruby on Rails 3,多个域的电子邮件验证?

ruby中愚蠢的哈希实验

Ruby 数组到 HTML 表,包括标题