[R] running R commands asynchronously

Martin Morgan mtmorgan at fhcrc.org
Sat Jun 11 19:09:00 CEST 2011


On 06/10/2011 02:29 PM, Richard M. Heiberger wrote:
> I am interested in running R commands asynchronously.
>
> My first choice is in the same R session that I am currently in.
> Here, the goal would be to run something like
>
>       RunAsynchSameSession(myfunction(), "outputname.rda")
>
> Once RunAsynchSameSession had started myfunction(),
> RunAsynchSameSession would complete immediately.  myfunction would
> keep going.  It is OK if execution of the myfunction() command
> prevents new input to R until it has completed.  The important feature
> is that RunAsynchSameSession must tell the progam that called it that
> it was done.

somewhere in-between, starting a new R session but keeping the 
communication within the original.

library(snow)
cl <- makeSOCKcluster("localhost")
sendCall(cl[[1]], function(n) { Sys.sleep(n); n }, list(n=5))
## do other things here...
recvResult(cl[[1]]) ## blocks until result available

I had hoped that I could open a non-blocking connection on the master 
(blocking=FALSE in the next-to-last line of newSOCKnode) and then poll 
with isIncomplete(cl[[1]]$con) but I couldn't get this to work (the 
connection, or more likely my understanding, seemed to be blocking anyway).

sendCall(cl[[1]], function(n) { Sys.sleep(n); n }, list(n=5))
while(isIncomplete(cl[[1]]$con))
     cat("tick\n")  ## not printed
recvResult(cl[[1]])

This approach might also work with the multicore package.

Martin

>
> Second choice is to start an independent process, BATCH or something
> similar, and save the resulting data objects in an .rda file.
>
>       RunAsynchBatch("myfile.r", "outputname.rda")
>
> The RunAsynchBatch would start a batch process and complete
> immediately after starting the batch file.  The batch file would run
> independently until it was completed.  While I know how to do this,
> for example with system(wait=FALSE), I would appreciate seeing a
> worked collection of statements, including getting outputname.rda back
> to the original R session.  I am working on Windows.
>
> Rich
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


-- 
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793



More information about the R-help mailing list