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

【字号: 日期:2022-10-21浏览:40作者:雯心

问题描述

#encoding:utf-8require ’net/http’require ’thread’require ’open-uri’queue = Queue.newthreads = []def get_html(url) open(url) do |f| #为什么没有执行到这边呢? puts f.read endend#add work to the queuequeue<<'http://www.cnblogs.com'10.times do threads<<Thread.new do until queue.empty? work_unit = queue.pop(true) rescue nil if work_unitputs get_html(work_unit) end end endend

问题解答

回答1:

解决办法:在末尾加上

threads.each{|t| t.join}

相关文章: