QuercusScriptEngine binding scope and thread safty

34 views
Skip to first unread message

ad...@cs.miami.edu

unread,
Feb 9, 2015, 9:48:43 PM2/9/15
to caucho-...@googlegroups.com
We would like to run Quercus from a servlet, but not use the defualt quercus servlet.  Essentially, we want to call a php function script when needed, and pipe the results to the servlet's "out.  In the following example, The engine is a singlton object that is shared among all instances of the servlet (some singleton code left out..).  We want to call the QuercusScriptEngine in a thread safe way, such that each time the script is run, the servlet's "out" is bound to the execution of the script.  Will the following code, in general, allow that to happen in a thread safe way (ie not overriding concurrently executing bindings).

      static QuercusScriptEngine engine=null;
       ....
       ....
      PrintWriter out = response.getWriter();

      if (engine == null) { // some singlton code left out here.
            engine = new QuercusScriptEngine();
        }
        Bindings bindings = engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
        bindings.put("out", out);

        if (phpCode == null) {
            phpCode = MyUtils.readFileToString(fileName);
        }
        engine.eval(phpCode);

Kaz Nishimura

unread,
Feb 10, 2015, 6:15:07 AM2/10/15
to caucho-...@googlegroups.com
You would have to use synchronized statements to access a shared resource exclusively. Another thread can create another QuercusScriptEngine after 'if (engine == null)' in a rare case, for example. 

ad...@cs.miami.edu

unread,
Feb 10, 2015, 9:49:24 AM2/10/15
to caucho-...@googlegroups.com
Hi,

If the QuercusScriptEngine is not thread safe, then yes we would have to synchronize runs of the engine (or create another engine), but it was my understanding that QuercusScriptEngine was thread safe?? (as in a single Engine can be called concurrently from multiple threads and the concurrent calls do not mess with each other. As long as the objects put in the bindings are not shared resources, (in this case a unique "out" is placed), then should it not be safe?

-Adam

Kaz Nishimura

unread,
Feb 10, 2015, 7:14:39 PM2/10/15
to caucho-...@googlegroups.com

I don't know how QuercusScriptEngine is thread-safe, but if you call 'engine.eval' and you make a binding before each call, the binding must be shared with other threads that uses the same 'engine'. How can you specify which binding the 'engine' uses? Thread safety does not mean each thread has its own bindings in the single 'engine'.

2015/02/10 23:49 <ad...@cs.miami.edu>:
--
You received this message because you are subscribed to the Google Groups "Quercus" group.
To unsubscribe from this group and stop receiving emails from it, send an email to caucho-quercu...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages