Method: Igo::Ja#async_query

Defined in:
lib/igo/ja.rb

#async_query(arr, timeout = 0, &block) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/igo/ja.rb', line 107

def async_query(arr, timeout=0, &block)
  promises = arr.map do |element|
    Concurrent::Promise.execute do
      begin
        Timeout.timeout(timeout) do # 设置最大执行时间为5秒
          block.call(element)
        end
      rescue Timeout::Error
        # 处理超时异常
        puts "任务执行超时, #{timeout} 秒!关键词参数 timeout: 指定超时秒数!"
        nil
      end
    end
  end

  # 等待所有任务完成
  results = promises.map(&:value!)

  # 获取结果数组
  results
end