Created
January 23, 2015 23:42
-
-
Save lnznt/113083fa0e0489b0bc7c to your computer and use it in GitHub Desktop.
Ruby: dRuby(drb) ref: http://qiita.com/lnznt/items/e0d5a43fde5c5ecaba67
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ pry -r drb # 'drb' を require する | |
: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pry の応答は、必要がない場合省いています。 | |
[1] pry(main)> front = [1,:foo,'hello'] # front オブジェクト(提供するオブジェクト) | |
[2] pry(main)> uri = 'druby://:12345' # サーバの URI | |
# (ホストを記述する例:'druby://192.168.1.101:12345') | |
[3] pry(main)> DRb.start_service(uri, front) # サービス開始(サーバはスレッドで動く) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1] pry(main)> class C | |
[1] pry(main)* attr_accessor :foo | |
[1] pry(main)* end | |
[2] pry(main)> obj = C.new | |
[3] pry(main)> Marshal.dump(obj) # Marshal.#dump できてしまう | |
=> "\x04\bo:\x06C\x00" | |
[4] pry(main)> class C # DRb::DRbUndumped を include | |
[4] pry(main)* include DRb::DRbUndumped | |
[4] pry(main)* end | |
[5] pry(main)> Marshal.dump(obj) # Marshal.#dump に失敗(例外があがる) | |
TypeError: can't dump | |
from /home1/lnznt/.rbenv/versions/2.1.5/lib/ruby/2.1.0/drb/drb.rb:389:in `_dump' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1] pry(main)> DRb.start_service 'druby://:12346', [{}] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1] pry(main)> array = DRbObject.new_with_uri 'druby://:12346' | |
[2] pry(main)> puts array[0] # 内容を確認 | |
{} # 空の Hash | |
[3] pry(main)> array[0][:foo] = 'hello' # キー :foo に値 "hello" 格納 | |
[4] pry(main)> puts array[0] # 再度、内容確認 | |
{} # !!! 変わっていない !!! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ここでは DRb::DRbUndumped は Hash インスタンスに直接 extend する | |
[1] pry(main)> DRb.start_service 'druby://:12347', [{}.extend(DRb::DRbUndumped)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1] pry(main)> array = DRbObject.new_with_uri 'druby://:12347' | |
[2] pry(main)> puts array[0] # 内容を確認 | |
{} # 空の Hash | |
[3] pry(main)> array[0][:foo] = 'hello' # キー :foo に値 "hello" 格納 | |
[4] pry(main)> puts array[0] # 再度、内容確認 | |
{:foo=>"hello"} # ちゃんと変わっている |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[14] pry(main)> robj = array.last # 分散オブジェクト取得 | |
=> #<DRb::DRbObject:0x007f54d8635978 | |
@ref=70085429798380, # これがリモートオブジェクトID | |
@uri="druby://127.0.0.1:12345"> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DRb.thread.join # サーバスレッドを join する(終了を待つ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ pry -r drb # 'drb' を require する | |
: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1] pry(main)> DRb.start_service # サービス開始 (URIなし) | |
# DRbObject インスタンスを作る。この時サーバの URI を指定する | |
[2] pry(main)> array = DRbObject.new_with_uri('druby://:12345') | |
[3] pry(main)> puts array # array はリモートオブジェクト | |
1 | |
foo | |
hello |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[4] pry(main)> array.class | |
=> DRb::DRbObject # Array クラスではない |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[5] pry(main)> array.count | |
=> 3 | |
[6] pry(main)> array.last | |
=> "hello" | |
[7] pry(main)> array.map {|x| "data is #{x}" } | |
=> ["data is 1", "data is foo", "data is hello"] | |
[8] pry(main)> p array # p (暗黙の inspect 呼び出し) には array 自身が答える | |
<DRb::DRbObject:0x007f54d7a91748 @uri="druby://:12345", @ref=nil> | |
[9] pry(main)> class << array # (あくまで、実験ですが) | |
[9] pry(main)* undef inspect # array.#inspect を undef してしまえば | |
[9] pry(main)* end # array 自身で答えられなくなり ... | |
[10] pry(main)> array.inspect # リモートの Array の応答が得られます | |
=> "[1, :foo, \"hello\"]" | |
[11] pry(main)> p array # (同じく) | |
[1, :foo, "hello"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[4] pry(main)> class C # クラスを定義 | |
[4] pry(main)* include DRb::DRbUndumped # !!! これがキモです !!! | |
[4] pry(main)* attr_accessor :foo | |
[4] pry(main)* end | |
[5] pry(main)> obj = C.new # インスタンス作成 | |
[6] pry(main)> obj.foo = "hello" # obj.foo に "hello" を設定 | |
[7] pry(main)> front << obj # obj を front に追加 | |
=> [1, :foo, "hello", #<C:0x007f7c124b4bd8 @foo="hello">] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[12] pry(main)> p array # インスタンスが追加されているのを確認してみる | |
[1, :foo, "hello", #<C:0x007f7c124b4bd8 @foo="hello">] | |
[13] pry(main)> p array.last.foo # foo を取得 | |
"hello" # "hello" が取得できた | |
[14] pry(main)> robj = array.last # 分散オブジェクト取得 | |
[15] pry(main)> robj.foo = "goodbye" # foo に "goodbye" を設定 | |
[16] pry(main)> p robj.foo # foo の 確認 | |
"goodbye" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[8] pry(main)> p front | |
[1, :foo, "hello", #<C:0x007f7c124b4bd8 @foo="goodbye">] | |
[9] pry(main)> p front.last.foo # obj.foo を取得 | |
"goodbye" # "goodbye" が取得できた |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment