When I use futures in plumber, such as the following interface, if many requests are sent at the same time, such as opening 10 browser tabs, the result is that the first 5 tabs are executed in sequence (start and end times are connected end to end), starting from The fifth start is multi-threaded, which can be seen from the start and end time of the return. Why are the first few requests not processed by multiple threads?
I'm going to adjust your example to print less information (as the start time can produce the end time) and use curl in parallel (from within the terminal) to remove any browser restrictions.
# ./plumber.R
library(future)
library(promises)
# fix the number of workers to 5
future::plan(future::multisession(workers = 5))
#' @param msg The message to echo back.
#' @serializer text
#' @get /echo
function(msg = ""){
future({
Sys.sleep(5)
paste0(Sys.getpid(), " - ", as.character(Sys.time()), "\n")
})
}
Code to run plumber
library(plumber)
pr("plumber.R") %>% pr_run(port = 8080)
#> Running plumber API at http://127.0.0.1:8080
#> Running swagger Docs at http://127.0.0.1:8080/__docs__/
Create urls file
# from within terminal
cat > urls.txt << EOF
http://localhost:8080/echo
http://localhost:8080/echo
http://localhost:8080/echo
http://localhost:8080/echo
http://localhost:8080/echo
http://localhost:8080/echo
http://localhost:8080/echo
http://localhost:8080/echo
http://localhost:8080/echo
http://localhost:8080/echo
EOF
Code to test plumber
# from within terminal
# Take all urls and execute curl in parallel with 10 workers
# We expect it to take two iterations with plumber, as only 5 workers are registered
cat urls.txt | xargs -P 10 -n 1 curl
#> 74504 - 2021-02-01 11:21:03
#> 74532 - 2021-02-01 11:21:03
#> 74558 - 2021-02-01 11:21:03
#> 74584 - 2021-02-01 11:21:03
#> 74610 - 2021-02-01 11:21:03
#> 74504 - 2021-02-01 11:21:08
#> 74532 - 2021-02-01 11:21:08
#> 74558 - 2021-02-01 11:21:09
#> 74584 - 2021-02-01 11:21:09
#> 74610 - 2021-02-01 11:21:09
The example above took two iterations. First batch at the 3s mark and the second batch at ~8-9s mark
Output above shows that the first five processes are all done in parallel right away.
Can you try the example above on your machine? Thank you!