⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 09 - sharing a hash between any number of computers.rb

📁 O Reilly Ruby Cookbook source code
💻 RB
字号:
#!/usr/local/ruby -w# drb_hash_server.rbrequire 'drb'# Start up DRb with a URI and a hash to shareshared_hash = {:server => 'Some data set by the server' }DRb.start_service('druby://127.0.0.1:61676', shared_hash)puts 'Listening for connection...'DRb.thread.join  # Wait on DRb thread to exit...#---require 'drb'# Prep DRbDRb.start_service# Fetch the shared objectshared_data = DRbObject.new_with_uri('druby://127.0.0.1:61676')# Add to the Hashshared_data[:client] = 'Some data set by the client'shared_data.each do |key, value|  puts "#{key} => #{value}"end# client => Some data set by the client# server => Some data set by the server#---# threadsafe_hash.rbrequire 'rubygems'require 'facet/basicobject'               # For the BasicObject classrequire 'thread'                          # For the Mutex class#---# A thread-safe Hash that delegates all its methods to a real hash.class ThreadsafeHash < BasicObject  def initialize(*args, &block)    @hash = Hash.new(*args, &block)  # The shared hash    @lock = Mutex.new                # For thread safety  end    def method_missing(method, *args, &block)    if @hash.respond_to? method  # Forward Hash method calls...      @lock.synchronize do       # but wrap them in a thread safe lock.        @hash.send(method, *args, &block)      end    else      super    end  endend#---#!/usr/bin/ruby -w# threadsafe_hash_server.rbrequire 'threadsafe_hash'  # both sides of DRb connection need all classesrequire 'drb'#---$SAFE = 1  # Minimum acceptable paranoia level when sharing code!#---# Start up DRb with a URI and an object to share.DRb.start_service('druby://127.0.0.1:61676', Threadsafe.new)puts 'Listening for connection...'DRb.thread.join  # wait on DRb thread to exit...#---#!/usr/bin/ruby# threadsafe_hash_client.rbrequire 'remote_hash'  # Both sides of DRb connection need all classesrequire 'drb'# Prep DRbDRb.start_service# Fetch the shared hash$shared_data = DRbObject.new_with_uri('druby://127.0.0.1:61676')puts 'Enter Ruby commands using the shared hash $shared_data...'require 'irb'IRB.start#---$ ruby threadsafe_hash_client.rbEnter Ruby commands using the shared hash $shared_data...irb(main):001:0> $shared_data.keys=> []irb(main):002:0> $shared_data[:terminal_one] = 'Hello other terminals!'=> "Hello other terminals!"#---$ ruby threadsafe_hash_client.rbEnter Ruby commands using the shared hash $shared_data...irb(main):001:0> $shared_data.keys=> [:terminal_one]irb(main):002:0> $shared_data[:terminal_one]=> "Hello other terminals!"irb(main):003:0> $shared_data[:terminal_two] = 'Is this thing on?'=> "Is this thing on?"#---irb(main):003:0> $shared_data.each_pair do |key, value|irb(main):004:1*   puts "#{key} => #{value}"irb(main):005:1> endterminal_one => Hello other terminals!terminal_two => Is this thing on?#---

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -