Class: TxTranslate::ParallelArray
- Inherits:
-
Object
- Object
- TxTranslate::ParallelArray
- Defined in:
- lib/tx_translate/parallel_array.rb
Instance Method Summary collapse
-
#initialize(array) ⇒ ParallelArray
constructor
平行处理.
- #parallel_process ⇒ Object
- #process_amount ⇒ Object
Constructor Details
#initialize(array) ⇒ ParallelArray
平行处理
14 15 16 17 18 |
# File 'lib/tx_translate/parallel_array.rb', line 14 def initialize(array) @content_array = array @result_array = [] @hydra = Typhoeus::Hydra.new end |
Instance Method Details
#parallel_process ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tx_translate/parallel_array.rb', line 20 def parallel_process @content_array.each_with_index do |item, i| text = '' request = Typhoeus::Request.new(TxTranslate::TencentFy.new(item).full_request_url.to_s, followlocation: true) request.on_complete do |response| json = JSON.parse(response.body) text = if json['Response']['Error'] TxTranslate::TencentFy.new(item).full_request_url else json['Response']['TargetText'] end puts text @result_array[i] = text end @hydra.queue(request) if i % process_amount == 0 @hydra.run elsif i == @content_array.size - 1 @hydra.run end end @result_array end |
#process_amount ⇒ Object
9 10 11 |
# File 'lib/tx_translate/parallel_array.rb', line 9 def process_amount TxTranslate.config[:process_amount] || 5 end |