process_revents keeps on raising exceptions

Issue #182 resolved
Noah Spies created an issue

I'm trying to run rpy2 interactively, through ipython, and I like it when my plot windows are resizable, so I've run the rpy2.interactive.process_revents.start() command. I keep on getting RuntimeError exceptions though. The following is sufficient to raise a RuntimeError:

from rpy2 import interactive
from rpy2 import robjects as ro
interactive.process_revents.start()
print str(ro.r.help("plot"))

I think the appropriate behavior would be to allow the process_revents thread to be started any number of times, ignoring second and subsequent calls to start().

Comments (7)

  1. Laurent Gautier

    The exception I am occasionally observing with the code snippet you provide (thanks for providing a self-contained example - makes things easier) is caused by a locking mechanism in rpy2 (there to prevent race conditions with respect to running R).

    R is rather unfriendly to multithreading and it was possible to break it badly with concurrent access to it (or at least it was the case when all this was implemented - I have not revisited the details since then). In order to prevent this from happening, a (crude) locking mechanism was implemented in rpy2: the lock is acquired when about to run R stuff, and is released when done. Failing to acquire the lock (because the lock is already held elsewhere results in an exception raised by rpy2:

    RuntimeError: Concurrent access to R is not allowed.
    

    Beside that interactive.process_revents.start() uses a singleton (so there is only one such thread). Calling start() several times should result in

    RuntimeError: Processing of R events already started.
    
  2. Noah Spies reporter

    Okay, that all sounds reasonable. I'm not sure why rpy2 is trying to concurrently access R under this scenario -- I guess the paging mechanism for help is conflicting with the background processing events thread? It's not that I need to run the help() function, but this was the simplest example of the error that I seemed to be getting with other code.

    Also, would be great if the singleton instance didn't actually raise this error:

    RuntimeError: Processing of R events already started.
    

    The exception seems unnecessary -- ordinarily, I'd expect the singleton to do what's right without the user having to know if she tried to instantiate it multiple times (I think).

  3. Laurent Gautier

    I did not get to look at this further, but I'd like to point out that there is an interface to the R doc pages from rpy2, although it is still a rough one.

    For example, try:

    from rpy2.robjects.packages import importr
    graphics = importr("graphics")
    print(graphics.plot.__doc__)
    

    Pull requests to improve the parsing of the R doc are obviously welcome.

  4. Laurent Gautier

    With iypthon/jupyter-notebook, accessing the R documentation is even simpler:

    from rpy2.robjects.packages import importr
    graphics = importr("graphics")
    help(graphics.plot)
    
    #or 
    
    ?graphics.plot
    
  5. Log in to comment