3

我有一个 Sinatra 应用程序和 DRb 服务器对象配对。当我尝试将 Sinatraparams散列传递给我的服务器对象上的一个方法时,我得到DRb::DRbConnError … DRb::DRbServerNotFound了,但是当我直接传递一个简单的散列时,相同的方法仍然有效。

  1. 为什么我在使用 Sinatra 参数哈希时收到此错误?
  2. 解决此问题的最简单和最正确的解决方法是什么?

这是一个简单的测试用例:

# server.rb
require 'drb'
class Server; def echo( hash ); hash; end; end
DRb.start_service 'druby://localhost:9007', Server.new
DRb.thread.join
# app.rb
require 'sinatra'
require 'drb'    
SERVER  = DRbObject.new_with_uri 'druby://localhost:9007'
get("/params"){ SERVER.echo(params).inspect        }
get("/hash"  ){ SERVER.echo(hello:'world').inspect }

这两个都在自己的进程中运行:

phrogz$ curl http://localhost:4567/hash
{:hello=>"world"}

phrogz$ curl http://localhost:4567/params
DRb::DRbConnError - DRb::DRbServerNotFound:
 /usr/local/lib/ruby/1.9.1/drb/drb.rb:1653:in `current_server'
 /usr/local/lib/ruby/1.9.1/drb/drb.rb:1721:in `to_id'
 /usr/local/lib/ruby/1.9.1/drb/drb.rb:1050:in `initialize'
 /usr/local/lib/ruby/1.9.1/drb/drb.rb:642:in `new'
 /usr/local/lib/ruby/1.9.1/drb/drb.rb:642:in `make_proxy'
 /usr/local/lib/ruby/1.9.1/drb/drb.rb:559:in `rescue in dump'
 /usr/local/lib/ruby/1.9.1/drb/drb.rb:556:in `dump'
 /usr/local/lib/ruby/1.9.1/drb/drb.rb:603:in `block in send_request'
 /usr/local/lib/ruby/1.9.1/drb/drb.rb:602:in `each'
 /usr/local/lib/ruby/1.9.1/drb/drb.rb:602:in `send_request'
 /usr/local/lib/ruby/1.9.1/drb/drb.rb:903:in `send_request'
 /usr/local/lib/ruby/1.9.1/drb/drb.rb:1196:in `send_message'
 /usr/local/lib/ruby/1.9.1/drb/drb.rb:1088:in `block (2 levels) in method_missing'
 /usr/local/lib/ruby/1.9.1/drb/drb.rb:1172:in `open'
 /usr/local/lib/ruby/1.9.1/drb/drb.rb:1087:in `block in method_missing'
 /usr/local/lib/ruby/1.9.1/drb/drb.rb:1105:in `with_friend'
 /usr/local/lib/ruby/1.9.1/drb/drb.rb:1086:in `method_missing'
 app.rb:4:in `block in <main>'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:1152:in `call'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:1152:in `block in compile!'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:724:in `instance_eval'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:724:in `route_eval'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:708:in `block (2 levels) in route!'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:758:in `block in process_route'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:755:in `catch'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:755:in `process_route'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:707:in `block in route!'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:706:in `each'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:706:in `route!'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:843:in `dispatch!'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:644:in `block in call!'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:808:in `instance_eval'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:808:in `block in invoke'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:808:in `catch'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:808:in `invoke'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:644:in `call!'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:629:in `call'
 /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.2/lib/rack/head.rb:9:in `call'
 /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.2/lib/rack/commonlogger.rb:18:in `call'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/showexceptions.rb:21:in `call'
 /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.2/lib/rack/methodoverride.rb:24:in `call'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:1272:in `block in call'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:1303:in `synchronize'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:1272:in `call'
 /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.2/lib/rack/content_length.rb:13:in `call'
 /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.2/lib/rack/chunked.rb:15:in `call'
 /usr/local/lib/ruby/gems/1.9.1/gems/thin-1.2.11/lib/thin/connection.rb:84:in `block in pre_process'
 /usr/local/lib/ruby/gems/1.9.1/gems/thin-1.2.11/lib/thin/connection.rb:82:in `catch'
 /usr/local/lib/ruby/gems/1.9.1/gems/thin-1.2.11/lib/thin/connection.rb:82:in `pre_process'
 /usr/local/lib/ruby/gems/1.9.1/gems/thin-1.2.11/lib/thin/connection.rb:57:in `process'
 /usr/local/lib/ruby/gems/1.9.1/gems/thin-1.2.11/lib/thin/connection.rb:42:in `receive_data'
 /usr/local/lib/ruby/gems/1.9.1/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
 /usr/local/lib/ruby/gems/1.9.1/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
 /usr/local/lib/ruby/gems/1.9.1/gems/thin-1.2.11/lib/thin/backends/base.rb:61:in `start'
 /usr/local/lib/ruby/gems/1.9.1/gems/thin-1.2.11/lib/thin/server.rb:159:in `start'
 /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.2/lib/rack/handler/thin.rb:14:in `run'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:1234:in `run!'
 /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/main.rb:25:in `block in <module:Sinatra>'

这是在 OS X 上的 Ruby 1.9.2 下运行的,我认为这并不重要。

4

1 回答 1

4

简短的回答

您需要添加

DRb.start_service

app.rb您尝试拨打远程电话之前。

有兴趣的说明

Sinatra 参数哈希是使用关联块创建的,以处理引用缺失键的情况(这里是源代码)。这意味着有一个Proc与之关联的对象。

Drb 通过编组来回传递参数。但是,来自Marshal 文档

某些对象不能被转储:如果要转储的对象包括绑定、过程或方法对象、类 IO 的实例或单例对象,则会引发 TypeError。

因此,尝试通过网络传递此参数散列将会出现问题,因为它包含一个不可编组的过程对象。

现在进入Drb 文档

然而,如果一个对象不能被编组,一个对它的 dRuby 引用将被传递或返回。这将在远程端作为 DRbObject 实例出现。在这个远程代理上调用的所有方法都被转发到本地对象,如 DRbObjects 的讨论中所述。这具有类似于普通 Ruby 传递引用的语义。

好消息,它应该仍然有效。那么有什么问题呢?在 Drb 文档中更进一步,我们在示例代码中找到了这一点:

# Start a local DRbServer to handle callbacks.
#
# Not necessary for this small example, but will be required
# as soon as we pass a non-marshallable object as an argument
# to a dRuby call.
DRb.start_service

因此,似乎正在发生的事情是 Drb 试图获取过程对象的远程引用以传递给服务器,但由于客户端没有设置 Drb 服务,因此无法。


原始答案

(我将把它留在这里,这可能是我感兴趣的。这是我弄清楚这一切的旅程中的一个休息点。这也是一种可能的替代解决方案。有趣的是,我现在似乎是所提到的更博学的人到,至少关于为什么。)

这是一个可能的解决方法。问题似乎出在使用块创建的散列以处理丢失的键(Sinatra 的 params 散列是),因此您可以将散列的内容提取到一个新的中。params.clone并且params.merge({})两者似乎都保留了 proc(您可以检查Hash#default_proc),但是{}.merge(params)(或merge!)为您提供了与 Drb 一起使用的干净的哈希值。

因此,在此示例中,请执行以下操作:

get("/params"){ SERVER.echo({}.merge params).inspect

为什么Drb 和带有 procs 的哈希会发生这种情况,以及这是否是最简单或最正确的解决方法,我将留给更有知识的人。

于 2011-06-05T17:09:05.450 回答