0

我对 Ruby 完全陌生,所以我的问题可能有一个非常简单的答案。但是,我在stackoverflow上找不到答案。我有以下非常简单的 Sinatra 应用程序:

# myapp.rb
require 'rubygems'
require 'sinatra'
require 'json'

range=(199..2000).step(1)

set :port, 8888

get '/hostname' do
  content_type :json
  return range.next.to_json
end

西纳特拉开始:

 ruby testsinatra.rb
== Sinatra/1.0 has taken the stage on 8888 for development with backup from WEBrick
[2014-09-11 08:43:18] INFO  WEBrick 1.3.1
[2014-09-11 08:43:18] INFO  ruby 1.8.7 (2011-06-30) [x86_64-linux]
[2014-09-11 08:43:18] INFO  WEBrick::HTTPServer#start: pid=8215 port=8888

并提供第一个请求:

 curl -ks http://localhost:8888/hostname
199

但在第二个请求中失败并出现错误:

RuntimeError - 跨线程调用的延续:

 /usr/lib/ruby/1.8/generator.rb:131:in `call'
 /usr/lib/ruby/1.8/generator.rb:131:in `next'
 /usr/lib/ruby/1.8/generator.rb:189:in `next'
 testsinatra.rb:30:in `GET /hostname'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:863:in `call'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:863:in `route'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:521:in `instance_eval'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:521:in `route_eval'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:500:in `route!'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:497:in `catch'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:497:in `route!'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:476:in `each'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:476:in `route!'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:601:in `dispatch!'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:411:in `call!'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:566:in `instance_eval'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:566:in `invoke'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:566:in `catch'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:566:in `invoke'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:411:in `call!'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:399:in `call'
 /usr/lib/ruby/gems/1.8/gems/rack-1.5.2/lib/rack/showexceptions.rb:24:in `call'
 /usr/lib/ruby/gems/1.8/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
 /usr/lib/ruby/gems/1.8/gems/rack-1.5.2/lib/rack/commonlogger.rb:33:in `call'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:979:in `call'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:1003:in `synchronize'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:1003:in `synchronize'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:979:in `call'
 /usr/lib/ruby/gems/1.8/gems/rack-1.5.2/lib/rack/handler/webrick.rb:60:in `service'
 /usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
 /usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
 /usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
 /usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
 /usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
 /usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
 /usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
 /usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
 /usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
 /usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
 /usr/lib/ruby/gems/1.8/gems/rack-1.5.2/lib/rack/handler/webrick.rb:14:in `run'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:946:in `run!'
 /usr/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/main.rb:25
 testsinatra.rb:27

显然,我缺少一些基本的东西。请指教。

4

1 回答 1

0

测试代码给出:

FiberError at /hostname
fiber called across threads

您可以在此处找到相关问题:Sharing an enumerator across threads。似乎Fiber代码存储了第一个访问该对象的线程的 ID,并且当另一个线程尝试这样做时立即失败。您显然不能在线程之间共享枚举器,并且必须采用不同的方法。

另请注意,不同线程可能同时访问全局变量,您应始终使用线程安全对象或显式锁定。

于 2014-10-27T05:46:53.193 回答