Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async server or quickly loading state in R

Tags:

r

I’m writing a webserver that sometimes has to pass data through a R script.

Unfortunately startup is slow, since i have to load some libraries which load other libraries etc.

Is there a way to either

  • load libraries, save the interpreter state to a file, and load that state fast when invoked next time? Or

  • maintain a background R process that can be sent messages (not just lowlevel data streams), which are delegated to asynchronous workers (i.e. sending a new message before the previous is parsed shouldn’t block)

R-Websockets is unfortunately synchronous.

like image 396
flying sheep Avatar asked Jan 29 '14 13:01

flying sheep


1 Answers

Rserve and RSclient is an easy way to do create and use an Async server.

Open two R sessions.

in the first one type:

require(Rserve)
run.Rserve(port=6311L)

in the second one type:

require(RSclient)
rsc = RS.connect(port=6311L)
# start with a synchronous call
RS.eval(rsc, {x <<- vector(mode="integer")}, wait=TRUE)
# continue with an asynchronous call
RS.eval(rsc, {cat("begin")
              for (i in 1:100000) x[[i]] <-i
              cat("end")
              TRUE
        }, 
        wait=FALSE)
# call collect until the result is available
RS.collect(rsc, timeout=1)
like image 54
tjean Avatar answered Oct 02 '22 10:10

tjean