asynchronous - starting a function in asynch mode in R, as a separate process -


i looking ability start r processes asynchronously within r. below function

startfunctioninasynchmode<-function(workingdir,filestosource, functionname, ...){  #workingdir - dir should set wd  #filestosource - vector of filenames sourced  #functionname - actual function run asynchrously  #...  - other parameters passed function  #return value - should system process id started } 

would have quick ideas? checked packages parallel etc. doesn't seem fit. in advance

here implementation using r cmd. basic version tested. , open items.


startfunctioninasynchmode<-function(workingdir,filestosource, functionname, ...){     wd<-getwd()     setwd(workingdir)     fs<-makefiles()     scriptfile<-fs$scriptfile     cat(file=scriptfile,paste0("source(\"",filestosource,"\")", collapse = "\n"))     cat(file=scriptfile,"\n",append = t)     functioncall<-getfunctioncall(functionname,as.list(match.call()), startindex=5)     cat(file=scriptfile,functioncall,append = t)     commandstorun <- paste0("(r cmd batch ", scriptfile, " ",fs$logfile , " --slave ) &")     print(commandstorun)     system(commandstorun)     sys.sleep(5)     pids<-getpids(scriptfile, "--restore")     cat(file=fs$killscript,paste0("kill -9 ",pids$pid[1]))     setwd(wd)     return(as.character(pids$pid[1])) }  makefiles<-function(){     res<-list()     dir.create("./temp/temprgen", recursive=t,showwarnings = f)     tf<-tempfile("rgen-","./temp/temprgen", fileext = "")     res$scriptfile<-paste0(tf,".r")     res$logfile<-paste0(tf,".log")     res$killscript<-paste0(tf,"-kill.sh")     file.create(res$killscript,showwarnings = f)     file.create(res$scriptfile,showwarnings = f)     res }  #open items handled #1. named arguments #2. non string arguments getfunctioncall<-function(functionname,arglist,startindex){     res<-paste0(functionname,"(")     if(!is.null(arglist)){         if(length(arglist)>=startindex){             first=t             for(i in startindex:length(arglist)){                 if(first){                     first=f                 } else {                     res<-paste0(res,",")                 }                 res<-paste0(res,"\"",arglist[[i]],"\"")             }         }     }     res<-paste0(res,")") }  getpids <- function(grepfor, refinewith){     numcols <- length(unlist(str_split(system("ps aux", intern=t)[1], "\\s+")))     psoutput <- system(paste0("ps auxww | grep ", grepfor), intern=t)     psoutput <- psoutput[str_detect(psoutput, refinewith)]     piddf <- ldply(psoutput, parseeachpsline)     # remove process grep-ed search string     piddf <- piddf[!str_detect(piddf$command, "grep"),]     return(piddf) }  parseeachpsline <- function(line){     tabular <- read.table(textconnection(line), header=f, sep=" ")     tabular <- tabular[!is.na(tabular)]     pstitles <- c("user", "pid", "cpu", "mem", "vsz", "rss", "tty", "stat", "start", "time", "command")     pscolnames <- setnames(seq(1, length(pstitles)), pstitles)      command <- paste0(tabular[(pscolnames["command"]):length(tabular)], collapse=" ")     return(data.frame("pid"=tabular[pscolnames["pid"]], "started"=tabular[pscolnames["start"]], "command"=command, "status"=tabular[pscolnames["stat"]])) } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -