Created
March 7, 2014 04:07
-
-
Save wppurking/9405074 to your computer and use it in GitHub Desktop.
使用多线程访问页面
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'HTTParty' | |
class PageView | |
# 引入 HTTParty gem | |
include HTTParty | |
# 发起模拟访问 | |
def visit_page | |
PageView.get("http://www.amazon.co.uk/dp/B006FRD8QU") | |
end | |
# 启动线程 | |
def start_thread | |
threads = [] | |
5000.times do |i| | |
threads[i] = Thread.new do | |
Thread.current[:name] = "Amazon vist page thread #{i + 1}" | |
visit_page | |
end | |
end | |
threads.each {|t| t.join; print t["name"], ","} | |
end | |
# 入口 | |
def do_it | |
ts = [] | |
# 线程开始 | |
ts << start_thread | |
end | |
end | |
# 新建实例并执行入口方法 | |
PageView.new.do_it |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment