Skip to content

Commit

Permalink
Merge pull request #2864 from takaram/patch-1
Browse files Browse the repository at this point in the history
timeout gem 0.2.0 での TimeoutError, Kernel.#timeout の削除を反映
  • Loading branch information
znz authored May 6, 2024
2 parents 10a237f + 6f68ae5 commit 80f62cb
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions refm/api/src/timeout.rd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ category Thread

タイムアウトを行うライブラリです。

#@until 3.1
= reopen Kernel

== Private Instance Methods
Expand Down Expand Up @@ -35,6 +36,7 @@ DNSの名前解決に時間がかかった場合割り込めません
その処理を Ruby で実装しなおすか C 側で Ruby
のスレッドを意識してあげる必要があります。
#@# [[unknown:timeoutの落し穴|trap::timeout]]も参照
#@end

= module Timeout

Expand Down Expand Up @@ -118,7 +120,7 @@ Socket などは DNSの名前解決に時間がかかった場合割り込めま
その処理を Ruby で実装しなおすか C 側で Ruby
のスレッドを意識してあげる必要があります。

以下の例では、gethostbyname(およそ0.6秒処理に時間がかかっている) が終了した直後((A)の箇所)で TimeoutError 例外があがっています。
以下の例では、gethostbyname(およそ0.6秒処理に時間がかかっている) が終了した直後((A)の箇所)で Timeout::Error 例外があがっています。

例 timeout が割り込めない
require 'timeout'
Expand All @@ -137,7 +139,7 @@ Socket などは DNSの名前解決に時間がかかった場合割り込めま
# 実行例
=> ["helium.ruby-lang.org", [], 2, "210.251.121.214"]
0.689331
/usr/local/lib/ruby/1.6/timeout.rb:37: execution expired (TimeoutError)
/usr/local/lib/ruby/1.6/timeout.rb:37: execution expired (Timeout::Error)
from -:6:in `timeout'
from -:6
# gethostbyname が0.1秒かからない場合は例外が発生しないので
Expand Down Expand Up @@ -215,21 +217,23 @@ timeout による割り込みは [[m:Kernel.#system]] によって呼び出さ
#@else
= class Timeout::Error < Interrupt
#@end
#@until 3.1
alias TimeoutError
#@end

[[lib:timeout]] で定義される例外クラスです。
関数 timeout がタイムアウトすると発生します。

[[lib:timeout]] を使うライブラリを作成する場合は、ユーザが指定した
timeout を捕捉しないようにライブラリ内で [[c:TimeoutError]] のサブクラスを
timeout を捕捉しないようにライブラリ内で Timeout::Error のサブクラスを
定義して使用した方が無難です。
#@#((-注: version 1.6 では、[[unknown:ruby-list:33352]] のパッチが必要です。
#@#このパッチは 1.7 に取り込まれました[[unknown:ruby-list:33391]]-))

==> foo.rb <==
require 'timeout.rb'
class Foo
FooTimeoutError = Class.new(TimeoutError)
FooTimeoutError = Class.new(Timeout::Error)
def longlongtime_method
Timeout.timeout(100, FooTimeoutError) {
...
Expand All @@ -239,7 +243,7 @@ timeout を捕捉しないようにライブラリ内で [[c:TimeoutError]] の

==> main.rb <==
require 'foo'
timeout(5) {
Timeout.timeout(5) {
Foo.new.longlongtime_method
}

Expand Down

0 comments on commit 80f62cb

Please sign in to comment.