ruby rest-client:让它永不超时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4435538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ruby rest-client: make it never timeout?
提问by earnold
I am trying to use ruby rest-clientto upload a large number of images to a site that I'm writing. My code looks like:
我正在尝试使用 ruby rest-client将大量图像上传到我正在编写的站点。我的代码看起来像:
RestClient.post url, :timeout => 90000000, :open_timeout => 90000000, :file_param => file_obj
However, I am getting this error:
但是,我收到此错误:
RestClient::RequestTimeout: Request Timeout
from /Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/request.rb:174:in `transmit'
from /Library/Ruby/
But when I look at the server log
但是当我查看服务器日志时
Completed in 61493ms (View: 2, DB: 1) | 201 Created
So there doesn't appear to be any reason why this is timing out. Anyone have any idea if there is a timeout param I am not correctly setting?
因此,这似乎没有任何理由超时。任何人都知道是否有我没有正确设置的超时参数?
Thanks
谢谢
回答by phlipper
This syntax sets the timeout as request header (see RestClient.post signature), if you want to use the timeout parameter you must use:
此语法将超时设置为请求标头(请参阅 RestClient.post 签名),如果要使用超时参数,则必须使用:
RestClient::Request.execute(:method => :post, :url => @url, :timeout => 90000000)
see: https://github.com/rest-client/rest-client/blob/master/lib/restclient/request.rb#L12
见:https: //github.com/rest-client/rest-client/blob/master/lib/restclient/request.rb#L12
回答by clemensp
Looking at the docs, you can pass -1 through RestClient.execute timeout param:
查看文档,您可以通过 RestClient.execute timeout 参数传递 -1 :
# * :timeout and :open_timeout passing in -1 will disable the timeout by setting the corresponding net timeout values to nil
It can be used as follows:
它可以按如下方式使用:
resource = RestClient::Resource.new(
"url",
:timeout => -1,
:open_timeout => -1
response = resource.get :params => {<params>}
回答by if __name__ is None
I already use RestClient.get and RestClient.post extensively, so for me, it was easier to 'Monkey Patch' RestClient. I would recommend using RestClient::Resource.newor RestClient::Request.Executeif possible.
我已经广泛使用 RestClient.get 和 RestClient.post,所以对我来说,'Monkey Patch' RestClient 更容易。如果可能的话,我会建议使用RestClient::Resource.new或RestClient::Request.Execute。
However, since I'm lazy, and don't want to go swap out every occurrence of RestClient.get/ RestClient.postin my code, I've decided to take a shortcut.
但是,由于我很懒惰,并且不想在我的代码中每次出现RestClient.get/时都换掉RestClient.post,所以我决定走捷径。
$timeout = 30
$open_timeout = 30
module RestClient2
include RestClient
def self.get(url, headers={}, &block)
Request.execute(:method => :get, :url => url, :headers => headers,
:timeout => $timeout, :open_timeout => $open_timeout, &block)
end
def self.post(url, payload, headers={}, &block)
Request.execute(:method => :post, :url => url, :payload => payload, :headers => headers,
:timeout => $timeout, :open_timeout => $open_timeout, &block)
end
end
And than I just just quick replaced RestClient.get/post with RestClient2.get/post.
而且我只是用RestClient2.get/post快速替换了RestClient.get /post。
It would be nice, if RestClient::Requesthad a default timeout specified, like:
如果RestClient::Request指定了默认超时,那就太好了,例如:
@timeout = args[:timeout] || 30
@open_timeout = args[:open_timeout] || 30
回答by Lu Tahmazyan
I have used following code and works like a charm as pointed out by Richard
正如理查德所指出的,我使用了以下代码并像魅力一样工作
resource = RestClient::Resource.new "url",
:timeout => $TIMEOUT,
:open_timeout => $OPEN_TIMEOUT
response = resource.get :params => { ..... }
回答by Mike Davis
I'm having similar issues. A quick dive into the source reveals this bit of unfriendliness:
我有类似的问题。快速深入了解源代码会发现这种不友好:
def self.post(url, payload, headers={}, &block)
Request.execute(:method => :post, :url => url, :payload => payload, :headers => headers, &block)
end
Unless I'm missing something, the timeout options aren't passed on to the underlying request. Time for a patch ...
除非我遗漏了什么,超时选项不会传递给底层请求。是时候打补丁了...
回答by Richard Anderson
The RestClient::Resource.new() allows you to set :timeout and :open_timeout values that will get passed to the Request.execute method, when you use the resource's get, post, put, etc methods
RestClient::Resource.new() 允许您设置 :timeout 和 :open_timeout 值,当您使用资源的 get、post、put 等方法时,这些值将传递给 Request.execute 方法

