很久不写了,发一个最近的ruby多线程例子,没有线程同步问题,比较简单。
- require 'json'
- require 'rest-client'
- require 'thread'
- $contenct=[]
- def get_metrics
- get_json($content).select do |item|
- if /^opentsdb/ =~ item
- item
- end
- end
- end
- def batch_request(thread_index, items, result)
- tmp = []
- items.each_with_index do |item, index|
- begin
- res = JSON.parse(RestClient.get "http://opentsdb.xxx.com/api/query?start=1w-ago&m=sum:1w-max:#{item}")
- rescue
- retry
- end
- puts "thread-#{thread_index}: #{index}:#{item}:#{res.size != 0}"
- tmp << "#{item},#{res.size != 0}"
- end
- result << tmp
- end
- def main
- metrics = get_metrics
- puts '*' * 144
- puts metrics.size
- groups = metrics.each_slice(400).to_a
- thread_jobs = []
- results = []
- groups.each_with_index do |items, index|
- thread_jobs << Thread.new { batch_request(index, items, results) }
- end
- thread_jobs.each { |thread| thread.join }
- puts '#' * 144
- records = 0
- open('data.csv', 'w+') do |file|
- results.each do |items|
- records += items.size
- items.each do |item|
- file << item + "\n"
- end
- end
- puts records
- end
- puts 'done'
- end
- main
12-17 19:05