robjects.r function hangs with mod_wsgi (apache2) ubuntu

Issue #136 invalid
Former user created an issue

robjects.r function hangs with mod_wsgi apache2 12.10.

The function works without any issue with runserver (Django) and hangs only with mod_wsgi.

Any pointers on how to resolve this issue is greatly appreciated.

Comments (16)

  1. Pavel D

    I am experiencing exactly the same issue! It's unfortunate too, because it worked fine in runserver....

  2. Laurent Gautier

    Did you try with something else than mod_wsgi (e.g., FastCGI) ? In other words are you sure this is mod-wsgi-specific, and not something general to your Apache configuration ?

  3. Pavel D

    I tried it 'fastcgi' with Gunicorn and Nginx, it worked. Here is the test view, the R code is minimal.

    def testr(request):
        import rpy2.robjects as r
        out = r.r("print('TEST')")
        return HttpResponse(out)
    

    I also tried multiprocessing Process and it would seem to run, but not do anything. If I tried to process.join() it would freeze the whole wsgi process. So I ended up with standalone script, subprocess and passing parameters from command line.

  4. Laurent Gautier

    Try breaking your test function down in order to pinpoint where the problem is.

    # low-level interface (will be used  to troubleshoot)
    import rpy2.rinterface as rinterface 
    from rpy2.robjects.packages import importr
    
    print("initializing...")
    rinterface.initr()
    print("  done.")
    
    print("Mapping the R base package...")
    base = importr('base')
    print("  done.")
    
    def testr(request):
       out = str(base.R_version)
       return HttpResponse(out)
    
  5. Laurent Gautier

    Others have got rpy2 to work with Apache, I think.

    I'd be interested to see whether there is something obviously caused by a problem in rpy2, or there is something specific to your configuration.

  6. Sula Rajapakse

    I have same issue. My code runs fine on Flask but using mod_wsgi behind Apache the rpy2 calls hang. Is there a way to figure out why it is hanging in Rpy2?

    Code Snippet>> try: rgetwd = robjects.r('getwd()') print " R working directory "+str(rgetwd) except Exception: print("R working directory is "+rgetwd)

    This runs fine under Flask

  7. Laurent Gautier

    Then I suspect that the issue has more to do with either configuration settings in Apache/mod_wsgi, or Apache/mod_wsgi itselft rather than rpy2...

  8. Sula Rajapakse

    What are the proper settings for mod_wsgi/Apache so Rpy2 will work? Specifically it hangs trying to source R functions.

  9. Petr Klus

    Hi,

    I am having the same problem with running rpy2 withing mod_wsgi. I've had initial problems with library imports, which I've replaced with import statements.

    However, I am now experiencing hangs when the contained R process throws RRuntimeError - on local host, I get the exception + trace, however, running inside apache I just get a hang :(

  10. Laurent Gautier

    R cannot multithread, and while rpy2 is trying to prevent this from doing harm there might be something happening with.

    Did/can you try bringing the threads to one (and increase processes instead) in your configuration directives ?

  11. Petr Klus

    Will try again with all the fixes. In the meantime, I've also found importing scipy troublesome and the only real solution was one explained below:

    mod_wsgi WSGIApplicationGroup

    My config now looks as follows:

    WSGIApplicationGroup %{GLOBAL} WSGIDaemonProcess xxx.com user=www-data group=www-data processes=4 threads=1

  12. Laurent Gautier

    Closing this as invalid, somehow. R cannot do multithreading and using web servers on the Python side that use are not going to work well.

    This is common to most C extensions in Python: unless specified otherwise they acquire the PIL and do not release it until the C code returns.

  13. Log in to comment