ruby 在单独的线程中使用多个连接进行http请求的简单类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 在单独的线程中使用多个连接进行http请求的简单类相关的知识,希望对你有一定的参考价值。

require "net/http"

#   fetcher = FeedCrawler::URLFetcher.new("example.com")
#   paths = ["/foo", "/bar", "/baz", "/qux"]
#   results = fetcher.fetch(paths)
#   results.each do |result|
#     puts result
#   end
#
class URLFetcher
  class WorkRequest
    def initialize(input)
      @input = input
      @mutex = Mutex.new
    end

    def process
      @mutex.synchronize do
        @output = yield @input
        @waiter.wakeup if @waiter
        @output
      end
    end

    def value
      @mutex.synchronize do
        return @output if defined? @output
        @waiter = Thread.current
        @mutex.sleep
        @output
      end
    end
  end

  def initialize(host, port=80, use_ssl: port == 443, workers: 2)
    @queue = Queue.new
    workers.times {start_worker(host, port, use_ssl)}
  end

  def fetch(paths)
    paths.map do |item|
      request = WorkRequest.new(item)
      @queue << request
      request
    end.each {|req| yield req.value}
  end

  private

  def start_worker(host, port, use_ssl)
    Thread.new do
      client = Net::HTTP.new(host, port)
      client.use_ssl = use_ssl
      client.start
      loop {@queue.pop.process {|path| do_request(client, path)}}
    end.abort_on_exception = true
  end

  def do_request(client, path)
    response = client.get(path)
    response.value
    response.body
  rescue
    tries = tries.to_i + 1
    client.finish
    client.start
    retry if tries < 3
    raise
  end
end

以上是关于ruby 在单独的线程中使用多个连接进行http请求的简单类的主要内容,如果未能解决你的问题,请参考以下文章

在进行数据库调用的线程中使用 ruby​​ 超时

Selector

java在线聊天项目1.1版 ——开启多个客户端,分别实现注册和登录功能,使用客户端与服务端信息request机制,重构线程,将单独的登录和注册线程合并

ruby 有真正的多线程吗?

IO和NIO的区别

多个 webRTC 连接