Skip to content
Prev 262459 / 398502 Next

running R commands asynchronously

On 06/10/2011 02:29 PM, Richard M. Heiberger wrote:
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