完成多个并行运行的R作业后如何运行另一个Rscript?

如何解决完成多个并行运行的R作业后如何运行另一个Rscript?

我需要如何运行脚本的安排是首先使用rstudioapi::jobRunScript()函数并行运行4个R脚本。并行运行的每个脚本不会从任何环境导入任何内容,而是将创建的数据帧导出到全局环境。 我的第5个R脚本基于并行运行的4个R脚本创建的数据帧,并且第5个R脚本也在控制台中运行。如果有一种方法可以在头四个R脚本并行运行后在后台在后台运行第5个脚本,而不是在控制台中运行,那会更好。我还试图减少整个过程的总运行时间。

尽管我能够弄清楚如何并行运行前四个R脚本,但是我的任务并未完全完成,因为我找不到如何触发第五个R脚本运行的方法。希望大家能在这里帮助我

解决方法

我喜欢这个有点太开放了。尽管rstudioapi绝对可以用于运行并行任务,但是它不是很通用,也不能提供非常有用的输出。 parallel Universe在R中很好地实现了几个包,这些包提供了一个更简单,更好的接口来实现此目的。这里有3个选项,它们也允许从不同的文件“输出”某些东西。

package =并行

使用并行包,我们可以非常简单地实现此目的。只需创建一个将要获取文件的向量,并在每个线程中执行source。当它们运行时,主进程将锁定,但是如果您无论如何都要等待它们完成,这并没有多大关系。

library(parallel)
ncpu <- detectCores()
cl <- makeCluster(ncpu)
# full path to file that should execute 
files <- c(...) 
# use an lapply in parallel.
result <- parLapply(cl,files,source)
# Remember to close the cluster
stopCluster(cl)
# If anything is returned this can now be used.

作为一个附带说明,一些软件包与parallel软件包(基于snow软件包构建)具有相似的接口,因此这是一个很好的基础。

package = foreach

parallel软件包的替代方案是foreach软件包,它提供类似于for-loop接口的内容,简化了接口,同时提供了更大的灵活性,并自动导入了必要的库和变量(尽管手动执行此操作比较安全)。
foreach软件包确实依赖paralleldoParallel软件包来建立集群。

library(parallel)
library(doParallel)
library(foreach)
ncpu <- detectCores()
cl <- makeCluster(ncpu)
files <- c(...) 
registerDoParallel(cl)
# Run parallel using foreach
# remember %dopar% for parallel. %do% for sequential.
result <- foreach(file = files,.combine = list,.multicombine = TRUE) %dopar% { 
  source(file)
  # Add any code before or after source.
}
# Stop cluster
stopCluster(cl)
# Do more stuff. Result holds any result returned by foreach.

虽然确实添加了几行代码,但是.combine.packages.export使得一个非常简单的界面可以在R中使用并行计算。

package = future

现在,这是要使用的较为罕见的软件包之一。 future提供的并行接口比parallelforeach都更灵活,允许异步并行编程。但是,该实现似乎有些艰巨,而我下面提供的示例只是对可能的实现进行了摸索。
还值得一提的是,尽管future软件包确实提供了运行代码所需的功能和软件包的自动导入,但经验使我意识到,这仅限于任何调用的第一级深度(有时更少),因此仍然需要导出。
尽管foreach依赖于parallel(或类似)来启动集群,但是foreach会使用所有可用的内核来启动自己。对plan(multiprocess)的简单调用将启动多核会话。

library(future)
files <- c(...) 
# Start multiprocess session
plan(multiprocess)
# Simple wrapper function,so we can iterate over the files variable easier
source_future <- function(file)
  future(file)
results <- lapply(files,source_future)
# Do some calculations in the meantime
print('hello world,I am running while waiting for the futures to finish')
# Force waiting for the futures to finish
resolve(results)
# Extract any result from the futures
results <- values(results)
# Clean up the process (close down clusters)
plan(sequential)
# Run some more code.

现在乍一看似乎很繁重,但是一般的机制是:

  1. 致电plan(multiprocess)
  2. 使用future(或%<-%,我将不介绍)执行某些功能
  3. 如果还有更多代码要运行,请执行其他操作,而这不取决于进程
  4. 使用resolve等待结果,该结果可用于列表(或环境)中的单个期货或多个期货。
  5. 在列表(或环境)中对单个期货使用value或对多个期货使用values收集结果
  6. 使用future清理在plan(sequential)环境中运行的所有群集
  7. 继续使用取决于您的期货结果的代码。

我相信这3个软件包为任何用户需要与之交互的多处理的每个必要元素(至少在CPU上)提供了接口。其他软件包提供了替代接口,而对于异步,我仅知道futurepromises。总的来说,我建议大多数用户在进行异步编程时要非常小心,因为与同步并行编程相比,这可能会引起一整套不那么频繁的问题。

我希望这可以为rstudioapi接口提供一个替代方案(非常严格),我可以肯定的是,它绝不打算由用户自己用于并行编程,而是更有可能用于执行任务例如通过界面本身并行构建软件包。

,

您可以将promisesfuture结合使用:promises::promise_all后跟promises::then可以等待上一个futures的完成,然后再启动最后一个{后台进程。
在作业在后台运行时,您可以控制控制台。

library(future)
library(promises)

plan(multisession)
# Job1
fJob1<- future({
  # Simulate a short duration job
  Sys.sleep(3)
  cat('Job1 done \n')
})

# Job2
fJob2<- future({
  # Simulate a medium duration job
  Sys.sleep(6)
  cat('Job2 done \n')
})


# Job3
fJob3<- future({
  # Simulate a long duration job
  Sys.sleep(10)
  cat('Job3 done \n')
})

# last Job
runLastJob <- function(res) {
  cat('Last job launched \n')
  # launch here script for last job
}

# Cancel last Job
cancelLastJob <- function(res) {
  cat('Last job not launched \n')
}

#  Wait for all jobs to be completed and launch last job
p_wait_all <- promises::promise_all(fJob1,fJob2,fJob3 )
promises::then(p_wait_all,onFulfilled = runLastJob,onRejected = cancelLastJob)

Job1 done 
Job2 done 
Job3 done 
Last job launched 
,

我不知道这对您当前的情况有多大的适应性,但是这是一种使四件事并行运行,获取它们的返回值然后触发第五个表达式/函数的方法。

前提是使用foreach (string pageName in _pages) { Task.Run(async () => { Sync_pageName() }); // where pageName will be the items from list. } 运行单个文件。这实际上运行的是callr::r_bg,而不是文件,因此我会修改这些文件的外观,例如

我将编写一个辅助脚本,该脚本旨在模仿您的四个脚本之一。我猜想您也希望能够正常获取该源代码(直接运行而不是作为函数运行),因此我将生成脚本文件,以便它“知道”它是源文件还是直接运行(基于Rscript detect if R script is being called/sourced from another script)。 (如果您知道function,则类似于python的python技巧。)

名为if __name__ == "__main__"的辅助脚本。

somescript.R

作为演示,如果在控制台上somefunc <- function(seconds) { # put the contents of a script file in this function,and have # it return() the data you need back in the calling environment Sys.sleep(seconds) return(mtcars[sample(nrow(mtcars),2),1:3]) } if (sys.nframe() == 0L) { # if we're here,the script is being Rscript'd,not source'd somefunc(3) } ,则此 just 定义函数(如果需要,可以定义多个函数),它不会在最后一个{ {1}}块:

source

但是如果我在终端中使用if来运行它,

system.time(source("~/StackOverflow/14182669/somescript.R"))
#                                   # <--- no output,it did not return a sample from mtcars
#    user  system elapsed 
#       0       0       0           # <--- no time passed

回到前提。像上面的Rscript一样重写脚本文件,而不是四个“脚本”。如果操作正确,则可以$ time /c/R/R-4.0.2/bin/x64/Rscript somescript.R mpg cyl disp Merc 280C 17.8 6 167.6 Mazda RX4 Wag 21.0 6 160.0 real 0m3.394s # <--- 3 second sleep user 0m0.000s sys 0m0.015s somescript.R进行不同的意图。

我将使用这个脚本四次,而不是四个脚本。这是我们要自动化的手动操作:

Rscript

我们可以使用

实现自动化
source

现在运行第五个函数/脚本。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-