尝试使用curl-multi gem发出HTTP请求时,函数'add_to_curl'出错

时间:2013-11-17 21:08:04

标签: ruby curl httpclient libcurl curl-multi

我正在尝试测试'curl-multi'gem来发出http请求:


puts Benchmark.measure {
  require 'curl-multi'
  # make multiple GET requests
  easy_options = {:follow_location => true}
  multi_options = {:pipeline => true}

  Curl::Multi.get("http://www.google.com/","http://www.google.com/","http://www.google.com/", easy_options, multi_options) do|easy|
    # do something interesting with the easy response
    puts easy.last_effective_url
  end
}

但是有一个严重的错误:


/Users/bmalets/.rvm/gems/ruby-1.9.3-p448/gems/curl-multi-0.2/lib/curl-multi.rb: In function ‘add_to_curl’:
/Users/bmalets/.rvm/gems/ruby-1.9.3-p448/gems/curl-multi-0.2/lib/curl-multi.rb:238: warning: ISO C90 forbids mixed declarations and code
/Users/bmalets/.rvm/gems/ruby-1.9.3-p448/gems/curl-multi-0.2/lib/curl-multi.rb:243: error: ‘struct RString’ has no member named ‘len’
/Users/bmalets/.rvm/gems/ruby-1.9.3-p448/gems/curl-multi-0.2/lib/curl-multi.rb: In function ‘c_select’:
/Users/bmalets/.rvm/gems/ruby-1.9.3-p448/gems/curl-multi-0.2/lib/curl-multi.rb:287: error: ‘struct RArray’ has no member named ‘len’
/Users/bmalets/.rvm/gems/ruby-1.9.3-p448/gems/curl-multi-0.2/lib/curl-multi.rb:288: error: ‘struct RArray’ has no member named ‘ptr’
/Users/bmalets/.rvm/gems/ruby-1.9.3-p448/gems/curl-multi-0.2/lib/curl-multi.rb:292: error: ‘struct RArray’ has no member named ‘len’
/Users/bmalets/.rvm/gems/ruby-1.9.3-p448/gems/curl-multi-0.2/lib/curl-multi.rb:293: error: ‘struct RArray’ has no member named ‘ptr’
/Users/bmalets/.rvm/gems/ruby-1.9.3-p448/gems/curl-multi-0.2/lib/curl-multi.rb:302: warning: implicit conversion shortens 64-bit value into a 32-bit value

我该如何解决?

1 个答案:

答案 0 :(得分:2)

tl; dr curl-multi RubyGem现在非常陈旧且无需维护:v0.2(上一版本)已于2008年6月3日发布。它与Ruby不兼容1.9。

你绝对应该考虑以下替代方案:

两者都支持libcurl's multi interface


这里的问题是你使用Ruby 1.9而curl-multi是为Ruby 1.8设计的。

同时Ruby C API发生了变化,例如错误'struct RString' has no member named 'len'来自于你在Ruby 1.8中的事实(见ruby.h):

struct RString {
    struct RBasic basic;
    long len;
    char *ptr;
    union {
        long capa;
        VALUE shared;
    } aux;
};

在Ruby 1.9中你有:

struct RString {
    struct RBasic basic;
    union {
        struct {
            long len;
            char *ptr;
            union {
                long capa;
                VALUE shared;
            } aux;
        } heap;
        char ary[RSTRING_EMBED_LEN_MAX + 1];
    } as;
};

换句话说,事情已经针对short strings进行了优化,不再分配堆。