curl.perform throws invalid easy handle error under multi-threading env

2.1k views Asked by At

I use curl of ruby gem curb to fetch multi urls under multi-threading env, but it throws "Invalid easy handle" exception, but if it runs one by one in single-threading env everything is ok.

module Http
  @@curl = Curl::Easy.new
  @@curl.timeout = 10 #second
  @@curl.follow_location = true
  @@curl.max_redirects = 3
  @@options = Robot::HttpOptions.new

  def self.fetch(url, options=@@options, type=Robot::Type::HTML)
    @@curl.url = URI.unescape(url)==url ? URI.escape(url) : url
    @@curl.headers["User-Agent"] = options.user_agent
    begin
      @@curl.perform
    rescue Curl::Err::CurlError
      raise
    end
  end
end

self.fetch is called in multi-threading env.

Can someone help me on the thing? Thank you.

1

There are 1 answers

2
Mat On BEST ANSWER

From the cURL documentation:

You must never share the same handle in multiple threads.