在官方文档看了下,好像openuri默认不支持timeout吧?
http://www.ruby-doc.org/stdlib-2.1.1/libdoc/open-uri/rdoc/OpenURI.html
如果不能设置timeout的话,有替代品吗?
ringa_lee2017-04-22 08:58:03
https://github.com/lostisland/faraday
conn.get do |req|
req.url '/search'
req.options.timeout = 5 # open/read timeout in seconds
req.options.open_timeout = 2 # connection open timeout in seconds
end
天蓬老师2017-04-22 08:58:03
No need to bother, just use Net::HTTP
uri=URI(query_uri)
Net::HTTP.get(uri,:read_timeout=>30)
This is the document introduction:
open_timeout[RW]
Number of seconds to wait for the connection to open. Any number may be used, including Floats for fractional seconds. If the HTTP object cannot open a connection in this many seconds, it raises a Net::OpenTimeout exception. The default value is nil .
By the way, please translate it:
open_timeout
Set the maximum waiting time when opening a connection. Its value can be any number, including very small floating point numbers. If the connected painting cannot be opened within the set time period, a Net::OpenTimeout exception will be thrown. The default value is nil, which means no time limit.
This will limit the request to be completed within 30 seconds.
There are also many other functions, such as setting proxy servers, CA certificates, ssl certificates, etc.
You can refer to the documentation for details, so I won’t go into details here.