非常教程

Ruby 2.4参考手册

DRb

Rinda::TupleSpace

父类:ObjectIncluded模块:DRb :: DRbUndumped,MonitorMixin

元组空间管理对其包含的元组的访问,从而确保满足互斥要求。

sec为写选择,采取,移动,读出并通知方法可以是若干秒或新生对象。

公共类方法

new(period=60)显示源

创建一个新的TupleSpace。period用于控制修改TupleSpace后多久查找死元组。

如果period在上次修改后几秒钟内没有发现死元组,TupleSpace将停止查找死元组。

调用超类方法MonitorMixin.new

# File lib/rinda/tuplespace.rb, line 437
def initialize(period=60)
  super()
  @bag = TupleBag.new
  @read_waiter = TupleBag.new
  @take_waiter = TupleBag.new
  @notify_waiter = TupleBag.new
  @period = period
  @keeper = nil
end

公共实例方法

move(port, tuple, sec=nil) { |template| ... } 显示源

移动tupleport

# File lib/rinda/tuplespace.rb, line 484
def move(port, tuple, sec=nil)
  template = WaitTemplateEntry.new(self, tuple, sec)
  yield(template) if block_given?
  synchronize do
    entry = @bag.find(template)
    if entry
      port.push(entry.value) if port
      @bag.delete(entry)
      notify_event('take', entry.value)
      return port ? nil : entry.value
    end
    raise RequestExpiredError if template.expired?

    begin
      @take_waiter.push(template)
      start_keeper if template.expires
      while true
        raise RequestCanceledError if template.canceled?
        raise RequestExpiredError if template.expired?
        entry = @bag.find(template)
        if entry
          port.push(entry.value) if port
          @bag.delete(entry)
          notify_event('take', entry.value)
          return port ? nil : entry.value
        end
        template.wait
      end
    ensure
      @take_waiter.delete(template)
    end
  end
end

notify(event, tuple, sec=nil) 显示源

注册通知event。返回一个NotifyTemplateEntry。有关如何侦听通知的示例,请参阅NotifyTemplateEntry。

应该是event:

'写'

一个元组被添加

'采取'

元组被采纳或移动

'删除'

一个元组被覆盖或过期后丢失

当NotifyTemplateEntry过期时,TupleSpace还会通知您'关闭'事件。

# File lib/rinda/tuplespace.rb, line 567
def notify(event, tuple, sec=nil)
  template = NotifyTemplateEntry.new(self, event, tuple, sec)
  synchronize do
    @notify_waiter.push(template)
  end
  template
end

read(tuple, sec=nil) { |template| ... } 显示源

读取tuple,但不删除它。

# File lib/rinda/tuplespace.rb, line 521
def read(tuple, sec=nil)
  template = WaitTemplateEntry.new(self, tuple, sec)
  yield(template) if block_given?
  synchronize do
    entry = @bag.find(template)
    return entry.value if entry
    raise RequestExpiredError if template.expired?

    begin
      @read_waiter.push(template)
      start_keeper if template.expires
      template.wait
      raise RequestCanceledError if template.canceled?
      raise RequestExpiredError if template.expired?
      return template.found
    ensure
      @read_waiter.delete(template)
    end
  end
end

read_all(tuple)显示源

返回所有的元组匹配tuple。不会删除找到的元组。

# File lib/rinda/tuplespace.rb, line 545
def read_all(tuple)
  template = WaitTemplateEntry.new(self, tuple, nil)
  synchronize do
    entry = @bag.find_all(template)
    entry.collect do |e|
      e.value
    end
  end
end

take(tuple, sec=nil, &block)显示源

移除了 tuple

# File lib/rinda/tuplespace.rb, line 477
def take(tuple, sec=nil, &block)
  move(nil, tuple, sec, &block)
end

write(tuple, sec=nil) 显示源

添加 tuple

# File lib/rinda/tuplespace.rb, line 450
def write(tuple, sec=nil)
  entry = create_entry(tuple, sec)
  synchronize do
    if entry.expired?
      @read_waiter.find_all_template(entry).each do |template|
        template.read(tuple)
      end
      notify_event('write', entry.value)
      notify_event('delete', entry.value)
    else
      @bag.push(entry)
      start_keeper if entry.expires
      @read_waiter.find_all_template(entry).each do |template|
        template.read(tuple)
      end
      @take_waiter.find_all_template(entry).each do |template|
        template.signal
      end
      notify_event('write', entry.value)
    end
  end
  entry
end

私有实例方法

create_entry(tuple, sec) 显示源

# File lib/rinda/tuplespace.rb, line 577
def create_entry(tuple, sec)
  TupleEntry.new(tuple, sec)
end

keep_clean() 显示源

删除死去的元组。

# File lib/rinda/tuplespace.rb, line 584
def keep_clean
  synchronize do
    @read_waiter.delete_unless_alive.each do |e|
      e.signal
    end
    @take_waiter.delete_unless_alive.each do |e|
      e.signal
    end
    @notify_waiter.delete_unless_alive.each do |e|
      e.notify(['close'])
    end
    @bag.delete_unless_alive.each do |e|
      notify_event('delete', e.value)
    end
  end
end

need_keeper?() 显示源

检查元组空间以查看是否需要清理。

# File lib/rinda/tuplespace.rb, line 631
def need_keeper?
  return true if @bag.has_expires?
  return true if @read_waiter.has_expires?
  return true if @take_waiter.has_expires?
  return true if @notify_waiter.has_expires?
end

notify_event(event, tuple) 显示源

通知所有注册的侦听器event状态改变tuple

# File lib/rinda/tuplespace.rb, line 605
def notify_event(event, tuple)
  ev = [event, tuple]
  @notify_waiter.find_all_template(ev).each do |template|
    template.notify(ev)
  end
end

start_keeper()显示源

创建一个线程,扫描元组空间的过期元组。

# File lib/rinda/tuplespace.rb, line 615
def start_keeper
  return if @keeper && @keeper.alive?
  @keeper = Thread.new do
    while true
      sleep(@period)
      synchronize do
        break unless need_keeper?
        keep_clean
      end
    end
  end
end
Ruby 2.4

Ruby 是一种面向对象、命令式、函数式、动态的通用编程语言,是世界上最优美而巧妙的语言。

主页 https://www.ruby-lang.org/
源码 https://github.com/ruby/ruby
版本 2.4
发布版本 2.4.1

Ruby 2.4目录

1.缩略 | Abbrev
2.ARGF
3.数组 | Array
4.Base64
5.基本对象 | BasicObject
6.基准测试 | Benchmark
7.BigDecimal
8.绑定 | Binding
9.CGI
10.类 | Class
11.比较 | Comparable
12.负责 | Complex
13.计算续体 | Continuation
14.覆盖 | Coverage
15.CSV
16.日期 | Date
17.日期时间 | DateTime
18.DBM
19.代理 | Delegator
20.摘要 | Digest
21.Dir
22.DRb
23.编码 | Encoding
24.枚举 | Enumerable
25.枚举 | Enumerator
26.ENV
27.ERB
28.错误 | Errors
29.Etc
30.期望值 | Exception
31.错误类 | FalseClass
32.Fiber
33.Fiddle
34.文件 | File
35.文件实用程序 | FileUtils
36.查找 | Find
37.浮点 | Float
38.Forwardable
39.GC
40.GDBM
41.GetoptLong
42.Hash
43.Integer
44.IO
45.IPAddr
46.JSON
47.Kernel
48.语言 | 3Language
49.记录 | Logger
50.编排 | Marshal
51.MatchData
52.数学 | Math
53.矩阵 | Matrix
54.方法 | Method
55.模型 | Module
56.监控 | Monitor
57. 互斥 | Mutex
58.Net
59.Net::FTP
60.Net::HTTP
61.Net::IMAP
62.Net::SMTP
63.NilClass
64.数字 | Numeric
65.对象 | Object
66.ObjectSpace
67.Observable
68.Open3
69.OpenSSL
70.OpenStruct
71.OpenURI
72.OptionParser
73.路径名 | Pathname
74.完整输出 | PrettyPrint
75.Prime
76.Proc
77.过程 | Process
78.PStore
79.PTY
80.队列 | Queue
81.随机 | Random
82.范围 | Range
83.合理的 | Rational
84.Readline
85.Regexp
86.Resolv
87.Ripper
88.RubyVM
89.Scanf
90.SDBM
91.SecureRandom
92.Set
93.Shell
94.信号 | Signal
95.Singleton
96.套接字 | Socket
97.字符串 | String
98.StringIO
99.StringScanner
100.结构 | Struct