4

Is it safe to call RPy functions in a multiprocessing environment and are there any multiprocessing issues regarding RPy that one should be aware of?

A simple example might be the following:

from multiprocessing import Pool
from rpy import *

def f(x):
    return r.mean(x)


if __name__ == '__main__':
    p = Pool(5)
    print sum(p.map(f, [range(1, 1000000), range(2, 2000000), range(3, 3000000)]))
Nixuz
  • 3,439
  • 4
  • 39
  • 44

2 Answers2

1

Seeing that multiprocessing spawns new python instance for each worker instance in the pool and they share no common resources -- including instances of the R process, -- chances are it is thread-safe. Best way is to test it and see.

ktdrv
  • 3,602
  • 3
  • 30
  • 45
  • Could you suggest a way I could test it? – Nixuz Apr 18 '11 at 21:05
  • If the above code runs and gives you the answer you expect, that's your test and it passes. For more complicated code, I would just write it following the guidelines in the module documentation. If it runs at all, you should be good. Multiprocessing is pretty idiot-proof so no worries -- if you get no errors, you've got it. :) – ktdrv Apr 18 '11 at 21:13
0

I use multiprocessing and rpy2 with no problems. Can't speak to rpy (v1) but chances are good that it'll work.

Noah
  • 21,451
  • 8
  • 63
  • 71