R shiny 中的并行处理,调用 Python 脚本
Parallel processing in R shiny, calling Python script
我正在尝试在 R shiny 中进行并行处理,我想执行的并行任务是调用 python 脚本。但是它不起作用并且无法将结果从 python 取回 R。
下面是示例 R shiny 和 Python 代码。
App.R
library(shiny)
library(reticulate)
library(doParallel)
library(foreach)
ui <- fluidPage(
# Application title
titlePanel("Sample Program"),
mainPanel(
uiOutput("txtValue")
)
)
server <- function(input, output) {
source_python("../../PythonCode/Multiprocessing/multip.py")
cl <- makeCluster(detectCores(), type='PSOCK')
registerDoParallel(cl)
result <- foreach(i=1:5) %dopar% fsq(i)
stopCluster(cl)
output$txtValue <- renderUI({
result
})
}
shinyApp(ui = ui, server = server)
Python代码(multip.py)
def fsq(x):
return x**2
错误信息与shiny
无关:
library(reticulate)
library(doParallel)
library(foreach)
library(parallel)
source_python("multip.py")
cl <- makeCluster(detectCores(), type = 'PSOCK')
registerDoParallel(cl)
# throws: Error in unserialize(socklist[[n]]) : error reading from connection
foreach(i = 1:5) %dopar% fsq(i)
stopCluster(cl)
我对此的解释是,不能像序列化 R 函数那样序列化 Python 函数。一个简单的解决方法是在循环中使用 source_python
:
library(doParallel)
library(foreach)
library(parallel)
cl <- makeCluster(detectCores(), type = 'PSOCK')
registerDoParallel(cl)
foreach(i = 1:5) %dopar% {
reticulate::source_python("multip.py")
fsq(i)
}
stopCluster(cl)
我正在尝试在 R shiny 中进行并行处理,我想执行的并行任务是调用 python 脚本。但是它不起作用并且无法将结果从 python 取回 R。 下面是示例 R shiny 和 Python 代码。 App.R
library(shiny)
library(reticulate)
library(doParallel)
library(foreach)
ui <- fluidPage(
# Application title
titlePanel("Sample Program"),
mainPanel(
uiOutput("txtValue")
)
)
server <- function(input, output) {
source_python("../../PythonCode/Multiprocessing/multip.py")
cl <- makeCluster(detectCores(), type='PSOCK')
registerDoParallel(cl)
result <- foreach(i=1:5) %dopar% fsq(i)
stopCluster(cl)
output$txtValue <- renderUI({
result
})
}
shinyApp(ui = ui, server = server)
Python代码(multip.py)
def fsq(x):
return x**2
错误信息与shiny
无关:
library(reticulate)
library(doParallel)
library(foreach)
library(parallel)
source_python("multip.py")
cl <- makeCluster(detectCores(), type = 'PSOCK')
registerDoParallel(cl)
# throws: Error in unserialize(socklist[[n]]) : error reading from connection
foreach(i = 1:5) %dopar% fsq(i)
stopCluster(cl)
我对此的解释是,不能像序列化 R 函数那样序列化 Python 函数。一个简单的解决方法是在循环中使用 source_python
:
library(doParallel)
library(foreach)
library(parallel)
cl <- makeCluster(detectCores(), type = 'PSOCK')
registerDoParallel(cl)
foreach(i = 1:5) %dopar% {
reticulate::source_python("multip.py")
fsq(i)
}
stopCluster(cl)