Rumah  >  Soal Jawab  >  teks badan

Ruby:此段代码中OpenURI中的open函数为什么没有执行

#encoding:utf-8
require 'net/http'
require 'thread'
require 'open-uri'

queue = Queue.new
threads = []
def get_html(url)
  open(url) do |f|
    #为什么没有执行到这边呢?
    puts f.read
  end
end

#add work to the  queue
queue<<"http://www.cnblogs.com"
10.times do
  threads<<Thread.new do
    until queue.empty?
      work_unit = queue.pop(true) rescue nil
      if work_unit
        puts get_html(work_unit)
      end
    end
  end
end


ringa_leeringa_lee2761 hari yang lalu814

membalas semua(1)saya akan balas

  • 黄舟

    黄舟2017-04-22 08:58:00

    Penyelesaian:
    Tambahkan

    pada penghujungnya
    threads.each{|t| t.join}
    

    balas
    0
  • Batalbalas