【问题标题】:How to set timeout in RestClient gem in Ruby?如何在 Ruby 的 RestClient gem 中设置超时?
【发布时间】:2012-05-16 00:57:20
【问题描述】:

我正在使用 RestClient gem,通过它对服务器进行 get 调用。问题是如何从客户端设置超时。

RestClient.get "http://127.0.0.1:7819/tokenize/word/stackoverflow"

我想设置为 10 秒。

提前致谢!!

【问题讨论】:

    标签: ruby timeout rest-client


    【解决方案1】:

    你不需要修改任何东西。你可以直接使用RestClient::Request,比如:

    RestClient::Request.execute(:method => :get, :url => url, :timeout => 10, :open_timeout => 10)
    

    但请记住,最坏的情况是 20 秒。

    查看其他帖子的答案https://stackoverflow.com/a/5445421/565999

    【讨论】:

    • 这应该是答案——恐怕我必须断言猴子补丁是邪恶的,因为它们具有意想不到的副作用。
    • nit:如果这两个值相同,则不必同时指定 timeout 和 open_timeout。超时值将用于 read_timeout 和 open_timeout。 github.com/rest-client/rest-client/blob/…
    • 使用RestClient::Resource时,可以试试RestClient::Resource.new('http://slow', :timeout => 10)
    猜你喜欢
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 2014-08-14
    • 2012-01-11
    • 2014-07-30
    • 1970-01-01
    相关资源
    最近更新 更多