Class: TxTranslate::ParallelArray
- Inherits:
-
Object
- Object
- TxTranslate::ParallelArray
- Defined in:
- lib/tx_translate/parallel_array.rb
Constant Summary collapse
- PROCESS_AMOUNT =
10
Instance Method Summary collapse
-
#initialize(array) ⇒ ParallelArray
constructor
平行处理.
- #parallel_process ⇒ Object
Constructor Details
#initialize(array) ⇒ ParallelArray
平行处理
10 11 12 13 14 |
# File 'lib/tx_translate/parallel_array.rb', line 10 def initialize(array) @content_array = array @result_array = [] @hydra = Typhoeus::Hydra.new end |
Instance Method Details
#parallel_process ⇒ Object
16 17 18 19 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 |
# File 'lib/tx_translate/parallel_array.rb', line 16 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 |