很久不写了,发一个最近的ruby多线程例子,没有线程同步问题,比较简单。

  1. require 'json'
  2. require 'rest-client'
  3. require 'thread'
  4. $contenct=[]
  5. def get_metrics
  6. get_json($content).select do |item|
  7. if /^opentsdb/ =~ item
  8. item
  9. end
  10. end
  11. end
  12. def batch_request(thread_index, items, result)
  13. tmp = []
  14. items.each_with_index do |item, index|
  15. begin
  16. res = JSON.parse(RestClient.get "http://opentsdb.xxx.com/api/query?start=1w-ago&m=sum:1w-max:#{item}")
  17. rescue
  18. retry
  19. end
  20. puts "thread-#{thread_index}: #{index}:#{item}:#{res.size != 0}"
  21. tmp << "#{item},#{res.size != 0}"
  22. end
  23. result << tmp
  24. end
  25. def main
  26. metrics = get_metrics
  27. puts '*' * 144
  28. puts metrics.size
  29. groups = metrics.each_slice(400).to_a
  30. thread_jobs = []
  31. results = []
  32. groups.each_with_index do |items, index|
  33. thread_jobs << Thread.new { batch_request(index, items, results) }
  34. end
  35. thread_jobs.each { |thread| thread.join }
  36. puts '#' * 144
  37. records = 0
  38. open('data.csv', 'w+') do |file|
  39. results.each do |items|
  40. records += items.size
  41. items.each do |item|
  42. file << item + "\n"
  43. end
  44. end
  45. puts records
  46. end
  47. puts 'done'
  48. end
  49. main


09-02 15:36