保持与rcurl的连接打开
我正在连接一台服务器以发出数千个请求。根据我所看到的,看起来RCurl在每次请求时都会打开到服务器的连接。我想知道是否有一种方法可以让连接在一定的时间内保持打开状态。下面是我在一秒内运行两个请求时看到的情况保持与rcurl的连接打开,r,rcurl,R,Rcurl,我正在连接一台服务器以发出数千个请求。根据我所看到的,看起来RCurl在每次请求时都会打开到服务器的连接。我想知道是否有一种方法可以让连接在一定的时间内保持打开状态。下面是我在一秒内运行两个请求时看到的情况 * About to connect() to gt-tradeview port 80 (#0) * Trying 192.168.141.136... * connected * Connected to gt-tradeview (192.168.141.136) port 80
* About to connect() to gt-tradeview port 80 (#0)
* Trying 192.168.141.136... * connected
* Connected to gt-tradeview (192.168.141.136) port 80 (#0)
> POST /House/TradeView/ajax/varys HTTP/1.1
User-Agent: RCurl
Host: gt-tradeview
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: application/json
Content-Length: 360
< HTTP/1.1 200 OK
< Content-Length: 1690
< Content-Type: application/json
< Server: Microsoft-HTTPAPI/2.0
< Date: Thu, 09 Jan 2014 14:27:49 GMT
<
* Connection #0 to host gt-tradeview left intact
* About to connect() to gt-tradeview port 80 (#0)
* Trying 192.168.141.136... * connected
* Connected to gt-tradeview (192.168.141.136) port 80 (#0)
> POST /House/TradeView/ajax/varys HTTP/1.1
User-Agent: RCurl
Host: gt-tradeview
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: application/json
Content-Length: 227
< HTTP/1.1 200 OK
< Content-Length: 195
< Content-Type: application/json
< Server: Microsoft-HTTPAPI/2.0
< Date: Thu, 09 Jan 2014 14:27:49 GMT
<
* Connection #0 to host gt-tradeview left intact
所以没有任何连接打开
这是我的卷发
mkURL <- function(x) {
names <- names(x)
s <- sprintf("%s=%s",names[1],x[1])
for (i in 2:length(names)) {
s <- sprintf("%s&%s=%s",s,names[i],x[i])
}
URLencode(s)
}
curl.opts = curlOptions(
httpheader = c(
'Content-Type' = "application/x-www-form-urlencoded; charset=UTF-8",
'Accept' = "application/json"
#'Accept-Encoding' = "gzip,deflate,sdch"
),
verbose = TRUE, #change to TRUE for server feedback
header = TRUE,
buffersize = 100000000000,
useragent = "RCurl" ###
)
什么是
r$curl()
?您是否最终调用了一次getCurlHandle
,然后重新使用返回的值?也许您可以将范围缩小一点(根据需要编辑您的问题),例如,缩小到导致问题的特定(可公开访问的)服务器/协议。这显然会重新使用连接--curlPerform(url=”http://stackoverflow.com/questions/21023439/keep-connection-open-with-rcurl“,nobody=TRUE,verbose=TRUE,curl=r$curl())
当您说它重复使用连接时,这是否意味着后续请求会更快?运行该选项后,showconnections()是否仍然不显示任何内容?如果是这样,showconnections()做什么?showconnections
是关于基本的R连接(file()
,url()
,等等,请参见?连接
);不幸的是,与RCurl无关。我通过重复curlPerform()
并查找报告“即将连接…”(创建新的curl连接)或“重新使用现有连接…”(重新使用以前建立的curl连接)的输出来衡量重用性。好的,所以可以安全地假设这个特定的服务器正在关闭连接,因为您唯一的更改是从不同的服务器请求?
mkURL <- function(x) {
names <- names(x)
s <- sprintf("%s=%s",names[1],x[1])
for (i in 2:length(names)) {
s <- sprintf("%s&%s=%s",s,names[i],x[i])
}
URLencode(s)
}
curl.opts = curlOptions(
httpheader = c(
'Content-Type' = "application/x-www-form-urlencoded; charset=UTF-8",
'Accept' = "application/json"
#'Accept-Encoding' = "gzip,deflate,sdch"
),
verbose = TRUE, #change to TRUE for server feedback
header = TRUE,
buffersize = 100000000000,
useragent = "RCurl" ###
)
curlPerform(url = paste0("http://",serverToHit,"/House/TradeView/ajax/varys"),
#curlPerform(url = "http://192.168.141.148/House/TradeView/ajax/varys",
postfields = mkURL(parameters),
.opts = curl.opts,
writefunction = r$update,
post = 1L,
curl = r$curl())