Search code examples
javarmultithreadingservletsrenjin

Use renjin in a servlet


I have trained a model in R and want to compute predictions in a servlet using renjin. The predictions are always using the same unique model.

To avoid spending resources instantiating renjin and loading the same model for each request, I am considering to instantiate renjin and load the model once, in a singleton object, when the servet container starts (in a ServletContextListener).

My question is: is it thread safe to proceed like that and how does it work with renjin when several prediction requests are done at the same time (using the same model loaded in the same instance)? What is the right way of doing this: shall I use a lock on the renjin instance to keep it thread safe? or create a pool of threads instead?


Solution

  • A single Renjin Session shouldn't be used to evaluate multiple scripts concurrently.

    You can however maintain pool of Renjin ScriptEngine objects, or use ThreadLocal to simply maintain one per thread, like I did for the RenjinServlet example.

    Synchronizing access to a single ScriptEngine instance would also work if you don't expect a high level of concurrent requests.

    Data can be shared Sessions, however, so if memory is an issue, then you can first load the model from the classpath or WEB-INF and then share with each of the ScriptEngine instances.