R 中的并行处理,调用 Python 脚本 [英] Parallel processing in R shiny, calling Python script

查看:51
本文介绍了R 中的并行处理,调用 Python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 R 中进行并行处理,我想做的并行任务是调用 python 脚本.但是它不起作用并且无法将结果从 python 取回 R.下面是示例 R 闪亮和 Python 代码.应用程序

I am trying to do parallel processing in R shiny, the parallel task which I want to do is a call to python script. However it does not work and not able to fetch the result back from python to R. Below is the sample R shiny and Python code. 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)

Python Code (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)     

我将其解释为不能序列化 Python 函数,因为可以序列化 R 函数.一个简单的解决方法是在循环中使用 source_python:

I interpret this such that one cannot serialize a Python function as one can serialize a R function. A simple workaround is to use source_python within the loop:

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 中的并行处理,调用 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆