Joh*_*hir 5 ruby multithreading
如果在一定时间后没有完成,我想要一个我创建的线程.有没有优雅和/或惯用的方式来做到这一点?现在我正在考虑做一个观察者线程:
def myfunc
t = Thread.new{
#do stuff
}
# watcher thread
Thread.new{
result = t.join(20) # join returns nil if it timed out
t.kill if result.nil?
}
# continue on asynchronously from both threads
end
Run Code Online (Sandbox Code Playgroud)
也许Timeout课程就是你所需要的.
def myfunc
Thread.new{
Timeout::timeout(20) {
#do stuff
}
}
# continue on asynchronously
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3603 次 |
| 最近记录: |