10 Feb 2009 08:02
[Rpy] [patch] the Big R Lock
Nathaniel Smith <njs <at> pobox.com>
2009-02-10 07:02:16 GMT
2009-02-10 07:02:16 GMT
Currently, calls into R via rpy2 are *mostly* protected by the Python
global interpreter lock. That means that calling rpy2 functions from
different threads *almost* just works (even though R is not itself
re-entrant). However, it happens from time to time that R calls back
into Python (most commonly, via the console print callback), and then
Python might release the GIL, so you get:
Thread 1:
acquire GIL
call R
call Python
release GIL
Thread 2:
acquire GIL
call R
blow up
Oh well. The only solution is to add a GIL-alike lock for R. (Well,
that or never call into R from different threads, but that is very
irksome. Or do locking manually, but that's even worse.) So here's a
tedious patch does that (and also cleans up the "is R initialized
yet?" checking that is currently copy-pasted all over the code). It
uses Python threading API, so it should work anywhere that Python does
(though testing is always good).
With the patch applied, all current tests continue to pass, and the
following code works without error (without the patch it crashes
whenever a reasonable amount of text is printed):
_r_polling_thread = None
(Continue reading)
RSS Feed