我需要对 Web 服务进行 API 调用以检索日期。为此,我创建了一个示例以熟悉赛璐珞。在这里,我将使用 openweather API 进行“培训”。最终目标是同时运行多个请求。
我的预订舱位 (booking.rb) 获取数据
require 'celluloid'
require 'open-uri'
require 'json'
class Booking
include Celluloid
def initialize
end
def parse(url)
p "back again"
begin
buffer = open(url).read
p JSON.parse(buffer)['cod']
rescue => e
"fuck"
end
end
end
这就是我运行它的方式:
require 'celluloid'
Celluloid.shutdown_timeout = 10
begin
pool = Booking.pool
%W(
http://api.openweathermap.org/data/2.5/weather?q=London,uk
http://api.openweathermap.org/data/2.5/weather?q=Berlin,de
http://api.openweathermap.org/data/2.5/weather?q=Munich,de
http://api.openweathermap.org/data/2.5/weather?q=Munich,de
http://api.openweathermap.org/data/2.5/weather?q=Munich,de
http://api.openweathermap.org/data/2.5/weather?q=Munich,de
http://api.openweathermap.org/data/2.5/weather?q=Munich,de
http://api.openweathermap.org/data/2.5/weather?q=Munich,de
http://api.openweathermap.org/data/2.5/weather?q=Munich,de
http://api.openweathermap.org/data/2.5/weather?q=Munich,de
).each_with_index do |weather, i|
p i
#Booking.new.async.parse(weather)
pool.future.parse(weather)
end
rescue => e
p "ex #{e}"
end
p "start"
现在,当我多次运行它时,我确实收到了不同的错误消息:
ruby run_booking.rb
0
1
2
3
4
5
6
7
8
9
"start"
D, [2015-06-11T21:20:06.351274 #33316] DEBUG -- : Terminating 9 actors...
E, [2015-06-11T21:20:16.356649 #33316] ERROR -- : Couldn't cleanly terminate all actors in 10 seconds!
➜ booking ruby run_booking.rb
0
1
2
3
4
5
6
7
8
9
"start"
D, [2015-06-11T21:22:19.172770 #33344] DEBUG -- : Terminating 9 actors...
W, [2015-06-11T21:22:19.173145 #33344] WARN -- : Terminating task: type=:finalizer, meta={:method_name=>:__shutdown__}, status=:receiving
Celluloid::TaskFiber backtrace unavailable. Please try `Celluloid.task_class = Celluloid::TaskThread` if you need backtraces here.
所以我想知道这里发生了什么?帮助表示赞赏。提前致谢