2010-12-14 76 views
15

我想用ruby rest-client將大量圖像上傳到我正在編寫的網站。我的代碼如下所示:ruby​​ rest-client:讓它永不超時?

RestClient.post url, :timeout => 90000000, :open_timeout => 90000000, :file_param => file_obj 

不過,我得到這個錯誤:

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/ 

但是,當我看着服務器日誌

Completed in 61493ms (View: 2, DB: 1) | 201 Created 

所以似乎沒有被任何理由爲什麼這是超時。任何人有任何想法,如果有超時參數,我沒有正確設置?

謝謝

回答

2

我有類似的問題。快速潛入源顯示此位不友好的:

def self.post(url, payload, headers={}, &block) 
    Request.execute(:method => :post, :url => url, :payload => payload, :headers => headers, &block) 
end 

除非我失去了一些東西,超時選項不會傳遞到底層的請求。時間補丁...

+4

稍微深入剖析顯示,雖然'GET','POST'和相關便利的方法確實不允許你通過':timeout'和':open_timout'選項,它們只是'Request.execute'的精簡包裝,它將接受它們。更好的做法是用調用「執行」來替換對包裝器的調用,而不是對猴子補丁,恕我直言。 – 2012-09-12 19:50:13

19

這句法將超時時間設置爲請求頭(見RestClient.post簽名),如果你想使用的超時參數必須使用:

RestClient::Request.execute(:method => :post, :url => @url, :timeout => 90000000) 

見:https://github.com/rest-client/rest-client/blob/master/lib/restclient/request.rb#L12

+1

什麼是請求?未初始化的常量 – 2014-03-07 21:51:39

+2

RestClient :: Request.execute(:method =>:post,:url => @url,:timeout => 90000000) – 2014-06-18 19:40:26

+1

並將任何參數發送到POST正文(我認爲':file_param' in OP)到':payload':'Request.execute(:method =>:post,:url => @url,:timeout => 90000000,:payload => {:file_param => file_obj})' – WiseOldDuck 2014-09-29 16:56:32

2

的RESTClient實現:: Resource.new()允許您設置:超時和:open_timeout,將獲得傳遞給Request.execute方法值,當您使用資源的GET,POST,PUT等方法

4

我已經使用了下面的代碼並且像ch一樣工作手臂由Richard

resource = RestClient::Resource.new "url", 
            :timeout => $TIMEOUT, 
            :open_timeout => $OPEN_TIMEOUT 

response = resource.get :params => { ..... } 
12

望着文檔指出,可以把-1到RestClient.execute超時PARAM:

# * :timeout and :open_timeout passing in -1 will disable the timeout by setting the corresponding net timeout values to nil 

可以使用如下:

resource = RestClient::Resource.new(
    "url", 
    :timeout => -1, 
    :open_timeout => -1 
response = resource.get :params => {<params>} 
+3

已被更新爲'nil'而不是-1。使用-1會記錄一條警告(但似乎可行)。 – WiseOldDuck 2014-09-29 16:58:00

3

我已經廣泛地使用RestClient.get和RestClient.post,所以對於我來說,'Monkey Patch'RestClient更容易。如果可能,我建議使用RestClient::Resource.newRestClient::Request.Execute

但是,由於我很懶,也不想在我的代碼中換掉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 

而我剛剛用RestClient2.get/post快速替換了RestClient.get/post。

這將是很好,如果RestClient::Request有一個默認的超時規定,如:

@timeout = args[:timeout] || 30 
    @open_timeout = args[:open_timeout] || 30