代码之家  ›  专栏  ›  技术社区  ›  Rappster

使用plumber的异步API终结点

  •  0
  • Rappster  · 技术社区  · 5 年前

    plumber ?

    除了这个,我没有找到关于这个主题的水管工的文档 example 还有这个 GitHub issue

    当我试图重现这个例子时,我得到了一个错误,R不知道如何转换a promise 进入之内 JSON (至少我认为问题是这样的):

    <simpleError: No method asJSON S3 class: promise>
    

    例子

    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")
    }
    

    假设这个代码存在于文件中 plumber.R ,那么您应该能够启动API服务器并使用

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

    一旦我试过端点 /async ,我的R控制台报告

    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>
    

    enter image description here

    免责声明

    我是新来的 future promises 只做了一半 https://rstudio.github.io/promises/ 但是。

    0 回复  |  直到 5 年前