3

How do I define asynchronous API endpoints in plumber?

I didn't really find plumber-specific documentation on the topic except this example and this GitHub issue

When I try to reproduce the example, I get an error that R doesn't know how to turn a promise into JSON (at least that's what I think the problem is):

<simpleError: No method asJSON S3 class: promise>

Example

library(promises)

sleep_count <- 5
# add 5 seconds of sleep time
add_async_sleep <- function(p) {
  n <- 20
  for (i in 1:(sleep_count * n)) {
    p <- then(p, function(value) {
      Sys.sleep(1/n)
      "" # return value
    })
  }
  p
}

# use name_ as a placeholder for name when there are extra args
time <- function(name_, name = name_) {
  paste0(name, ": ", Sys.time())
}

new_promise <- function() {
  promise(function(resolve, reject){ resolve(NULL) })
}

#' @get /async
function() {
  new_promise() %>%
    add_async_sleep() %...>%
    time("async")
}

Say this code lives in file plumber.R, then you should be able to start the API server and bring up Swagger with

r <- plumber::plumb(here::here("plumber.R"))
r$run()

Once I try out the endpoint /async, my R console reports

Starting server to listen on port 7361
Running the swagger UI at http://127.0.0.1:7361/__swagger__/
<simpleError: No method asJSON S3 class: promise>

and Swagger looks like this:

enter image description here

Disclaimer

I'm new to future and promises and only made it mid-way through the docs on https://rstudio.github.io/promises/ yet.

Rappster
  • 12,762
  • 7
  • 71
  • 120
  • Have you found a solution to your problem? – Econ_matrix Mar 28 '20 at 22:27
  • @Econ_matrix yes in the sense that I've found something (based on `future::plan(future.callr::callr)`) that works and no in the sense that it still feels really hacky. I will try to put together a little gist of what I did. Ping me here once more in case you haven't heard from me until the end of the week – Rappster Mar 31 '20 at 13:33
  • Thank you. I would be happy to see your solution. I solved my problem which was very similar to what you had :), but now I am dealing with another difficulty namely HTTP post request. – Econ_matrix Mar 31 '20 at 20:06

0 Answers0