Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocking and waiting in R

Tags:

r

blocking

It would be extremely useful to me to be able to create a function in R that could block until a resource is defined or is given the appropriate value. I know that R is single-threaded, but I had hoped that mc would be able to help. However,

library(parallel)
f = function() {
  while(!exists('a')) Sys.sleep(1);
  print('success!')
}
d = mcparallel(f())
a = 1
mccollect(d)

hangs indefinitely. Is there any effective workaround, or will I have to look into radically different patterns/a different language to achieve something of the sort?

like image 978
tresbot Avatar asked Feb 06 '13 01:02

tresbot


1 Answers

Another hack, with little to recommend it, is to adapt the example at the bottom of ?socketConnection communicating between the two processes using sockets. We make the forked process the server (the server has to be started first, so can't be the interactive process) and send it on its way...

f <- function() {
    system.time({
        con1 <- socketConnection(port = 6011, server = TRUE, open="r")
        while (isIncomplete(con1))
            readLines(con1)
        close(con1)
    })
}     
d <- mcparallel(f())

Then we communicate with it, once only, and collect the results

con2 <- socketConnection(Sys.info()["nodename"], port = 6011)
writeLines("ok", con2)
close(con2)
mccollect(d)

which shows me that the forked process waited for a second and a half while I executed subsequent commands

> mccollect(d)
$`28975`
   user  system elapsed 
  0.000   0.000   1.515 

This would have a more legitimate feel if the processes were separate rather than forked, as in an MPI job where one could use the Rmpi package to communicate between nodes.

like image 102
Martin Morgan Avatar answered Sep 26 '22 22:09

Martin Morgan