我想使用Plumber包来执行一些灵活的并行处理,并希望它能在node.js框架中工作,这样它就不会阻塞……
我有以下水管工档案。

# myfile.R

#* @get /mean
normalMean <- function(samples=10){
  Sys.sleep(5)
  data <- rnorm(samples)
  mean(data)
}

我也安装了这里建议的pm2http://plumber.trestletech.com/docs/hosting/
我也做了同样的run-myfile.sh文件。
#!/bin/bash
R -e "library(plumber); pr <- plumb('myfile.R'); pr\$run(port=4000)"

并按建议执行…
我已经开始使用PM2
pm2 start /path/to/run-myfile.sh

想测试一下它是否能实现一个非阻塞的框架…
打开另一个R控制台并运行以下命令…
foo <- function(){
    con <- curl::curl('http://localhost:4000/mean?samples=10000',handle = curl::new_handle())
    on.exit(close(con))
    return(readLines(con, n = 1, ok = FALSE, warn = FALSE))
}

system.time(for (i in seq(5)){
    print(foo())
})

也许这是我对node.js非阻塞框架如何工作的误解,但在我的头脑中,最后一个循环应该只需要5秒多一点。但这似乎需要25秒,表明一切都是连续的,而不是平行的。
我怎么能用水管工包装来实现这种非阻塞性呢?

最佳答案

不幸的是,pm2不能为您负载平衡r进程。r是单线程的,没有真正的库允许它以异步方式运行,就像nodejs所做的那样(现在),所以现在在plumber中没有很多类似的并行化代码的好方法。最好的选择是运行多个水管工后端,并在它们之间分配流量。请参阅此处的“负载平衡”部分:http://plumber.trestletech.com/docs/docker-advanced

关于r - R plumber软件包,用于node.js并行处理,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38709615/

10-10 23:40