Is there any way to parallelize with plumber in windows?

58 views Asked by At

I want to parallelize a task with plumber, handling multiple requests at the same time. I have found an example that I have put in practice in windows and it doesn't work, but taking the same example to linux it does get the parallel calculation. Is there any way to get the parallelization in windows using the future library? or using doParallel + foreach

The example i used: text

# ./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")
  })}
library(plumber)
pr("plumber.R") %>% pr_run(port = 8080)

When I run this code in windows and send multiple requests to it, the process runs serially, as if the future package does not work.

How I tested:

library(httr)
responses <- lapply(1:6, function(i) {
  GET("http://127.0.0.1:8080/echo")
})

print(responses)

And the results:

[[1]]
Response [http://127.0.0.1:8080/echo]
  Date: 2024-01-23 08:37
  Status: 200
  Content-Type: text/plain; charset=UTF-8
  Size: 34 B
31320 - 2024-01-23 09:37:27.24171

[[2]]
Response [http://127.0.0.1:8080/echo]
  Date: 2024-01-23 08:37
  Status: 200
  Content-Type: text/plain; charset=UTF-8
  Size: 35 B
31320 - 2024-01-23 09:37:32.457364

[[3]]
Response [http://127.0.0.1:8080/echo]
  Date: 2024-01-23 08:37
  Status: 200
  Content-Type: text/plain; charset=UTF-8
  Size: 35 B
31320 - 2024-01-23 09:37:37.667452

[[4]]
Response [http://127.0.0.1:8080/echo]
  Date: 2024-01-23 08:37
  Status: 200
  Content-Type: text/plain; charset=UTF-8
  Size: 35 B
31320 - 2024-01-23 09:37:42.834899

[[5]]
Response [http://127.0.0.1:8080/echo]
  Date: 2024-01-23 08:37
  Status: 200
  Content-Type: text/plain; charset=UTF-8
  Size: 35 B
31320 - 2024-01-23 09:37:47.994111

[[6]]
Response [http://127.0.0.1:8080/echo]
  Date: 2024-01-23 08:37
  Status: 200
  Content-Type: text/plain; charset=UTF-8
  Size: 35 B
31320 - 2024-01-23 09:37:53.170176

I got the same pid as you can see.

I would like to know if it is possible to run this on windows.

Thank you

0

There are 0 answers