【问题标题】:Customize IO stream timeout value in Ruby / Rails在 Ruby / Rails 中自定义 IO 流超时值
【发布时间】:2013-07-28 07:30:21
【问题描述】:

在我的 rails 应用程序中,我使用 open-uri 打开一个外部文件,这可能需要 10 分钟才能加载

例子:

dl_stream = open('http://wetten.overheid.nl/xml.php?regelingID=bwbr0020368')

现在,1 分钟后,Ruby 将抛出超时错误。我从 \net\protocol.rc 的源代码中收集到了这个:

@read_timeout = 60

def rbuf_fill
  begin
    @rbuf << @io.read_nonblock(BUFSIZE)
  rescue IO::WaitReadable
    if IO.select([@io], nil, nil, @read_timeout)
      retry
    else
      raise Timeout::Error
    end
  rescue IO::WaitWritable
    # OpenSSL::Buffering#read_nonblock may fail with IO::WaitWritable.
    # http://www.openssl.org/support/faq.html#PROG10
    if IO.select(nil, [@io], nil, @read_timeout)
      retry
    else
      raise Timeout::Error
    end
  end
end

我猜我可以在我的应用设置中将此超时值设置为更适合我的情况的值,例如 15 分钟,但是如何以及在哪里?

【问题讨论】:

    标签: ruby-on-rails ruby timeout


    【解决方案1】:

    您可以使用:read_timeout 选项将超时(以秒为单位)添加到对open 的调用中:

    # timeout after 10 minutes
    open('http://example.com', :read_timeout => 600).read
    

    所有选项都记录在here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-27
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多