问题描述
我使用的执行和解析的whois查询。现在的问题是:我需要执行更多。成千上万。我发现异步包,这听起来非常适合我。这是简单的插件,它依赖于红宝石纤维和。
I'm using whois to perform and parse whois queries. The problem is: i need to perform much more. Thousands and thousands. I found async package em-whois, which sounds perfect for me. This is simple addon for whois, which relies on Ruby Fibers and em-synchrony.
我目前的code来执行的whois查询,看起来像这样:
My current code to perform whois queries looks like this:
...
c = Whois::Client.new(:timeout => timeout)
r = c.query(domain)
...
我已经安装了后,我试图延长的。
我的code看起来现在这个样子,仍然是很慢的:
My code looks like this now, and still is very slow:
$: << File.dirname(__FILE__) + '/../lib'
require 'em-whois'
EM.synchrony do
domain = ARGV[0] || "github.com"
$i = 0;
$num = 100;
arr = Array.new($num)
# perform all queries
begin
puts("Inside the loop i = #$i" );
$i += 1;
arr[$i] = Whois.whois(domain);
end while $i < $num
$i = 0;
# get all results
begin
$i += 1;
puts "\r#{domain}\n#{"-" * domain.length}"
[:available?, :status, :expires_on].each do |k|
puts "#{k}: #{arr[$i].properties[k]}"
end
end while $i < $num
EM.stop
end
我如何使用以及异步动力执行批处理查询(域1K) ?
How do i perform batch queries (1k of domains) using async power of whois and em-whois ?
以前我不与任何红宝石经验。我的Python / C / PHP开发人员。请帮助。
I don't have any experience with ruby before. I'm Python/C/PHP developer. Please help.
推荐答案
从图书馆的作者回答。例如链接 href=\"https://github.com/mikejarema/em-whois/blob/master/examples/async_parallel_whois.rb\" rel=\"nofollow\">。
Answer from author of the library. Example linked here.
$: << File.dirname(__FILE__) + '/../lib'
require 'em-whois'
require 'atomic'
# Asynchronous, parallel multi-domain WHOIS lookup
domains = ARGV.empty? ? ["github.com", "google.com", "bing.com", "yahoo.com", "mikejarema.com"] : ARGV
whois = {}
EM.synchrony do
# Progress indicator
EM.add_periodic_timer(0.1) do
STDERR.print "."
end
# Exit condition
EM.add_periodic_timer(0.1) do
if domains.size == whois.keys.size
puts ""
whois.each do |k,v|
if v.properties[:available?]
puts "#{k}: available"
else
puts "#{k}: taken, expires #{v.properties[:expires_on]}"
end
end
EM.stop
end
end
# Async WHOIS lookup - fire and forget via parallel fibers
domains.each do |i|
Fiber.new do
whois[i] = Whois.whois(i)
end.resume
end
end
这篇关于红宝石异步的whois的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!