为什么使用赛璐oid会出现随机错误? [英] Why do I get random errors by using celluloid?
问题描述
我需要对Web服务进行API调用才能检索日期。
为此,我创建了一个样本以熟悉赛璐oid。
在这里,我将使用openweather API进行培训。
最终目标是同时运行多个请求。
I need to make API calls to a webservice in order to retrieve date. For this purpose I created a sample in order to get familiar with celluloid. Here I ll user the openweather API for "training" purpose. The ultimate goal is to run multiple requests in concurrently.
我的预订类(booking.rb)获取数据
my booking class (booking.rb) fetches data
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"
现在,当我多次运行它时,我会收到不同的错误消息:
Now I do get different error messages when I run it several times:
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.
所以我想知道这是怎么回事吗?感谢帮助。
预先感谢
So I am wondering what's going on here? Help is appreciated. Thanks in advance
推荐答案
0。您的程序在赛璐oid
开始工作之前就完成了。
您需要使用#2中所示的方法来避免那。您需要分别加入
每个未来
,以确保检索并解析了所有信息...否则,您将被解雇大量不被阻塞
的异步调用...这意味着没有什么可以阻止程序退出。
0. Your program is finishing before Celluloid
does its work.
You need to use the method shown in #2 to avoid that. You need to join
each Future
to make sure all the information is retrieved and parsed... Otherwise you just fired off a ton of asynchronous calls that do not block
... which means nothing stops the program from exiting.
何时毫无疑问,只需在程序末尾添加 sleep
,直到您弄清楚如何优雅地完成。到目前为止,您显示的代码不完整。
When in doubt, just add sleep
to the end of your program, until you can figure out how you want to gracefully finish. So far the code you showed is incomplete.
- https://github.com/celluloid/celluloid/wiki/0.17.0-Prerelease
在您的Gemfile中:
In your Gemfile:
gem 'celluloid', github: 'celluloid/celluloid', branch: '0.17.0-prerelease', submodules: true
然后,当您<< c $ c>需要赛璐oid 库时,请使用以下命令:
Then when you require
the Celluloid
library, use this:
require 'celluloid/current'
2。只需使用期货。您根本不需要游泳池,它会降低您的速度:
2. Just use Futures. You don't need a pool at all, it will slow you down:
results = []
booking = Booking.new
%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
results << booking.future.parse(weather)
end
#de Turn the futures into actual results by calling `.value` on each future:
results = results.map(&:value)
3。将 http.rb
与赛璐oid
支持一起使用:
- https://github.com/httprb/http.rb#celluloidio -support
- https://github.com/httprb/http.rb#celluloidio-support
3. Use http.rb
with Celluloid
support:
将 Celluloid :: IO
添加到您的 Gemfile
像这样:
gem 'celluloid-io', github: 'celluloid/celluloid-io', branch: '0.17.0-dependent', submodules: true
然后请使用 HTTP
并传入 Celluloid :: IO
套接字类型。以下是 http.rb
本身的示例:
Then use HTTP
instead, and pass in Celluloid::IO
socket types. Here's the example from http.rb
itself:
require "celluloid/io"
require "http"
class HttpFetcher
include Celluloid::IO
def fetch(url)
HTTP.get(url, socket_class: Celluloid::IO::TCPSocket)
end
end
该调用使用事件TCP,与您的具有并发出站收集调用的actor配合使用。
That call right there uses evented TCP, which goes great with your actor which has concurrent outbound gathering calls.
这篇关于为什么使用赛璐oid会出现随机错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!