R 如何根据请求列表对 asnyc curl 响应进行排序/重新排序?

问题描述 投票:0回答:0

使用函数 curl_fetch_multi 我可以进行异步 http 调用。

multi_add 调度在调用 multi_run 时并发执行的请求。对于每个成功的请求,都会使用响应数据触发 done 回调。对于失败的请求(当 curl_fetch_memory 会引发错误时),会触发失败函数并显示错误消息。

问题是:我需要按照我构建 curl_fetch_multi 调用的顺序在 data 中获得响应。

举个例子:

data <- list()
success <- function(res){
  cat("Request done! Status:", res$status, "\n")
  data <<- c(data, list(res))
}
failure <- function(msg){
  cat("Oh noes! Request failed!", msg, "\n")
}

request_list<-c("http://google.de","http://httpbin.org/status/418","https://bing.com")

handles<-lapply(request_list,function(x)curl::curl_fetch_multi(x,success, failure))

curl::multi_run()
str(data)

数据中的响应顺序应该是:

  1. “http://google.de”
  2. “http://httpbin.org/status/418”
  3. “https://bing.com”

但是我运行我的代码时顺序是随机的(作为响应):

List of 6
 $ :List of 7
  ..$ url        : chr "http://httpbin.org/status/418"
  ..$ status_code: int 418
  ..$ type       : chr NA
  ..$ headers    : raw [1:257] 48 54 54 50 ...
  ..$ modified   : POSIXct[1:1], format: NA
  ..$ times      : Named num [1:6] 0.0 2.1e-05 2.6e-05 5.4e-05 9.7e-02 ...
  .. ..- attr(*, "names")= chr [1:6] "redirect" "namelookup" "connect" "pretransfer" ...
  ..$ content    : raw [1:135] 0a 20 20 20 ...
 $ :List of 7
  ..$ url        : chr "https://www.bing.com:443/?toWww=1&redig=84D1B48D3B1948B4AD35DC4B4C69E5CE"
  ..$ status_code: int 200
  ..$ type       : chr "text/html; charset=utf-8"
  ..$ headers    : raw [1:3231] 48 54 54 50 ...
  ..$ modified   : POSIXct[1:1], format: NA
  ..$ times      : Named num [1:6] 0.040574 0.000046 0.000055 0.0002 0.116532 ...
  .. ..- attr(*, "names")= chr [1:6] "redirect" "namelookup" "connect" "pretransfer" ...
  ..$ content    : raw [1:113883] 3c 21 64 6f ...
 $ :List of 7
  ..$ url        : chr "http://www.google.de/"
  ..$ status_code: int 200
  ..$ type       : chr "text/html; charset=ISO-8859-1"
  ..$ headers    : raw [1:740] 48 54 54 50 ...
  ..$ modified   : POSIXct[1:1], format: NA
  ..$ times      : Named num [1:6] 0.100166 0.00007 0.00008 0.000177 0.25349 ...
  .. ..- attr(*, "names")= chr [1:6] "redirect" "namelookup" "connect" "pretransfer" ...
  ..$ content    : raw [1:13833] 3c 21 64 6f ...
 $ :List of 7
  ..$ url        : chr "http://www.google.de/"
  ..$ status_code: int 200
  ..$ type       : chr "text/html; charset=ISO-8859-1"
  ..$ headers    : raw [1:741] 48 54 54 50 ...
  ..$ modified   : POSIXct[1:1], format: NA
  ..$ times      : Named num [1:6] 0.11156 0.00481 0.01488 0.01499 0.257 ...
  .. ..- attr(*, "names")= chr [1:6] "redirect" "namelookup" "connect" "pretransfer" ...
  ..$ content    : raw [1:13890] 3c 21 64 6f ...
 $ :List of 7
  ..$ url        : chr "https://www.bing.com:443/?toWww=1&redig=4AE372C6991F4873A7C8D284E45599E2"
  ..$ status_code: int 200
  ..$ type       : chr "text/html; charset=utf-8"
  ..$ headers    : raw [1:3233] 48 54 54 50 ...
  ..$ modified   : POSIXct[1:1], format: NA
  ..$ times      : Named num [1:6] 0.14163 0.00135 0.01223 0.19156 0.27674 ...
  .. ..- attr(*, "names")= chr [1:6] "redirect" "namelookup" "connect" "pretransfer" ...
  ..$ content    : raw [1:113883] 3c 21 64 6f ...
 $ :List of 7
  ..$ url        : chr "http://httpbin.org/status/418"
  ..$ status_code: int 418
  ..$ type       : chr NA
  ..$ headers    : raw [1:257] 48 54 54 50 ...
  ..$ modified   : POSIXct[1:1], format: NA
  ..$ times      : Named num [1:6] 0 0.000699 0.091084 0.091118 1.357831 ...
  .. ..- attr(*, "names")= chr [1:6] "redirect" "namelookup" "connect" "pretransfer" ...
  ..$ content    : raw [1:135] 0a 20 20 20 ...

我尝试通过匹配 url 基于 ** 句柄 ** 对列表列表 (data) 进行排序,但问题是; handles 是一个指针列表。

我是否可以使用一个选项来按调用顺序进行响应?

r curl libcurl
© www.soinside.com 2019 - 2024. All rights reserved.