1

In clojure I can do something like this:

(def x 
  ;; perform some expensive computation in a new thread
  ;; the repl is not blocked, so you can go on do something else
  (future 
    (do 
      (Thread/sleep 500) 
      3.14)))
;; ... do something else
;; now when you need x
;; just deref the future, get 3.14
@x    

Is there something similar to this in R?

qed
  • 22,298
  • 21
  • 125
  • 196
  • I wasn't aware there was an easy way to do multiple threads in R. Some discussion here: http://stackoverflow.com/questions/10835122/multithreading-with-r. I am curious if there is a good solution though. – EconomiCurtis May 24 '15 at 23:15

1 Answers1

2

On Linux you can fork a process and then collect it later, as illustrated on the help page ?parallel::mccollect(); this is more of a hack than a robust feature like future.

> p <- mcparallel({ Sys.sleep(5); "done" })
> sqrt(1:5)
[1] 1.000000 1.414214 1.732051 2.000000 2.236068
> mccollect(p)
$`15666`
[1] "done"

One can implement a similar strategy with snow::sendCall() / snow::recvResult(); see the implementation of snow::clusterCall() and note that a cluster can be subset, e.g., cl[1] to set a single node to work. (identically named functions are available in the parallel package, but not exported).

To check whether a job has finished, use wait = FALSE. Example:

require(parallel)
p1 = mcparallel({Sys.sleep(90); list(tag = "job1", res = 1})
p2 = mcparallel({Sys.sleep(80); 2})
p3 = mcparallel({Sys.sleep(60); 3})
res = mccollect(list(p1, p2, p3), wait = FALSE)
is.null(res)

If none of these 3 jobs has been finished, then res is NULL.

qed
  • 22,298
  • 21
  • 125
  • 196
Martin Morgan
  • 45,935
  • 7
  • 84
  • 112
  • In what sense is this not robust? – qed May 25 '15 at 07:37
  • 1
    @qed maybe I'm too cynical. mcparallel / mccollect are not cross-platform, for instance, and miscellaneous issues that seem to occur, e.g., for me mcparallel(quit()) seems to disrupt R's connections so that help like `?mcparallel` complains about not being able to open connections (!?), etc. – Martin Morgan May 25 '15 at 11:54
  • I see, that's not cynical at all, fair point. Akward support for concurreny/parallelism in R is one of the reasons I am gradually switching my workflow to clojure, which is a wonderful language. – qed May 25 '15 at 12:25