RubyTapas #134 - #140
134 Rake Clean
★★
コンバート
sh "ebook-convert book.html #{t.name}"
sh "kindlegen book.epub -o #{t.name}"
Rake clean task
require "rake/clean"
CLEAN # => []
CLEAN.include("book.html")
CLOBBER
CLOBBER << "book.epub"
CLOBBER << "book.mobi"
$ rake clobber
clobber first executes the clean task
135 Rake Multitask
★
CLEAN FileList
LISTINGS = FileList["listings/*"]
HIGHLIGHTS = LISTINGS.ext(".html")
CLEAN.include(HIGHLIGHTS)
rake内で task -> multitask
-j はプロセス数
multitask :highlight => HIGHLIGHTS
$ time rake -j 4
multitaskしなくても -m でできる
task :highlight => HIGHLIGHTS
$ time rake -m
136 Dead Thread
★
silent failure 問題があってもエラーにならずに気付かない
次のフラグを追加
Thread.abort_on_exception = true
debug
$DEBUG = true
timeout 通常は使うべきでない
require "timeout"
Timeout.timeout(3) do
multi threadはsilent failureを起こしやすい
137 Mutex
?
exclusivity
threadはjoinで起動
threads.each(&:join)
Mutex
Thread.exclusive
↓
@lock = Mutex.new
...
@lock.synchronize
concurrency難い
138 Condition Variable
?
previous episode : inefficient and CPU-intensive way for a thread to wait
only wait if the items array is empty
@item_available = ConditionVariable.new
...
@item_available.signal
@item_available.wait(@lock) if @items.empty?
now have a queue which is largely equivalent in functionality to Ruby's standard library Queue
前のエピソードより断然速い
139 Timed Queue
?
one second is an eternity in CPU terms
waitとかは1じゃなくて少数を使うべき
cv.wait(l, 0.05)
今まで必要でやったこともないし、あまり興味のあるジャンルじゃない
ただベンチマーク的な物は好き
140 Threads are Hard
?
shutdown
trap("INT") do
$shutdown = true
end
BUGを直す
deadline = timeout == :never ? :never : Time.now + timeout
...
loop do
cv_timeout = timeout == :never ? nil : deadline - Time.now
...
elsif deadline == :never || deadline > Time.now
Nightmare