如何在不等待响应的情况下发送GET请求 [英] How can I send a GET request without waiting for the response
问题描述
我正在尝试在ShinyApp内运行GET请求,但我不想等待响应,因为这将花费很长时间来处理,并且我并不需要在ShinyApp内真正需要响应,尽管状态代码会很好,但不是必须的。
I am trying to run a GET request inside a shinyApp, but I don't want to wait for the response as it would take quite a long time to process and I dont need really need the response inside the shinyApp, although a status code would be nice, but it is not obligatory.
或者是否有一个函数可以发送异步请求?就像将整个GET包装在一个Future / Promise中一样?
Or is there maybe a function, that sends an async request? Like wrapping the whole GET inside a future/promise?
当前,我在我的ShinyApp中有这个watchEvent:
Currently I have this observeEvent in my shinyApp:
observeEvent(input$import, {
httr::GET(url = "https://someurl/that/takes/a/long/time")
})
是 curl_fetch_multi
c $ c> curl 软件包适合该任务吗?
Is the curl_fetch_multi
from the curl
package suited for that task?
推荐答案
这是一种运行 GET
以会话内非阻塞方式(观察者不返回任何内容)异步获取:
Here is a way to run GET
asynchronously and in a intra-session non-blocking manner (observer returning nothing):
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)
这是对我的答案的略微修改此处。
This is a slight modification of my answer here.
这篇关于如何在不等待响应的情况下发送GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!