如何在不等待响应的情况下发送 GET 请求
How can I send a GET request without waiting for the response
我正在尝试 运行 shinyApp 中的 GET 请求,但我不想等待响应,因为这需要很长时间才能处理,而且我真的不需要响应在 shinyApp 中,虽然状态代码会很好,但它不是强制性的。
或者是否有发送异步请求的函数?就像将整个 GET 包装在 future/promise?
中一样
目前我的 shinyApp 中有这个 observeEvent:
observeEvent(input$import, {
httr::GET(url = "https://someurl/that/takes/a/long/time")
})
curl
包中的 curl_fetch_multi
是否适合该任务?
这里有一种方法 运行 GET
异步和会话内非阻塞方式(观察者不返回任何内容):
library(shiny)
library(future)
library(promises)
library(future.callr)
library(httr)
plan(callr)
queryGoogle <- function(queryString) {
myResponse <- httr::GET("http://google.com/", path = "search", query = list(q = queryString))
return(myResponse)
}
ui <- fluidPage(
br(),
textOutput("time_output"),
br(),
textInput(inputId="query_input", label = NULL, value = "", placeholder = "Search google..."),
actionButton("import", "Query"),
hr(),
textOutput("query_output")
)
server <- function(input, output, session) {
futureData <- reactiveValues(response = NULL)
observeEvent(input$import, {
myFuture <- future({
queryGoogle(isolate(input$query_input))
})
then(
myFuture,
onFulfilled = function(value) {
futureData$response <- value
},
onRejected = NULL
)
return(NULL)
})
output$query_output <- renderPrint({
req(futureData$response)
})
time <- reactive({
invalidateLater(500, session)
Sys.time()
})
output$time_output <- renderText({ paste("Something running in parallel:", time()) })
}
shinyApp(ui, server)
这是对我的回答的轻微修改 。
还请仔细阅读Joe Cheng的相关回答here。
我正在尝试 运行 shinyApp 中的 GET 请求,但我不想等待响应,因为这需要很长时间才能处理,而且我真的不需要响应在 shinyApp 中,虽然状态代码会很好,但它不是强制性的。
或者是否有发送异步请求的函数?就像将整个 GET 包装在 future/promise?
中一样目前我的 shinyApp 中有这个 observeEvent:
observeEvent(input$import, {
httr::GET(url = "https://someurl/that/takes/a/long/time")
})
curl
包中的 curl_fetch_multi
是否适合该任务?
这里有一种方法 运行 GET
异步和会话内非阻塞方式(观察者不返回任何内容):
library(shiny)
library(future)
library(promises)
library(future.callr)
library(httr)
plan(callr)
queryGoogle <- function(queryString) {
myResponse <- httr::GET("http://google.com/", path = "search", query = list(q = queryString))
return(myResponse)
}
ui <- fluidPage(
br(),
textOutput("time_output"),
br(),
textInput(inputId="query_input", label = NULL, value = "", placeholder = "Search google..."),
actionButton("import", "Query"),
hr(),
textOutput("query_output")
)
server <- function(input, output, session) {
futureData <- reactiveValues(response = NULL)
observeEvent(input$import, {
myFuture <- future({
queryGoogle(isolate(input$query_input))
})
then(
myFuture,
onFulfilled = function(value) {
futureData$response <- value
},
onRejected = NULL
)
return(NULL)
})
output$query_output <- renderPrint({
req(futureData$response)
})
time <- reactive({
invalidateLater(500, session)
Sys.time()
})
output$time_output <- renderText({ paste("Something running in parallel:", time()) })
}
shinyApp(ui, server)
这是对我的回答的轻微修改
还请仔细阅读Joe Cheng的相关回答here。