Class: Helper::AsyncCommand
- Inherits:
-
Object
- Object
- Helper::AsyncCommand
- Defined in:
- lib/helper.rb
Overview
外部コマンド実行中の待機ループの処理を書けるクラス
response = Helper::AsyncCommand.exec(“処理に時間がかかる外部コマンド”) do
print "*"
end if response.success?
puts "成功しました"
end
Class Method Summary collapse
Class Method Details
.exec(command, sleep_time = 0.5, &block) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/helper.rb', line 271 def self.exec(command, sleep_time = 0.5, &block) Thread.new { loop do block.call if block sleep(sleep_time) end }.tap { |th| begin if Helper.engine_jruby? # MEMO: # Open3.capture3 - 全く動かない # `` バッククウォート - 出力が文字化けする res = Open3.popen3(command) { |i, o, e| i.close `cd` # create dummy Process::Status object to $? [o.read, e.read, $?] } else res = Open3.capture3(command) end ensure th.kill end return res } end |