RESTful Webservice with embedded IronPython: engine & scope questions

668 views Asked by At

I have a RESTful C# web service (using Open Rasta) that I want to run IronPython scripts that talk to a CouchDB.

One thing I could use some clarification on is: How often do I need a new instance of the python engine and the scope? one each per application? per session? per request?

I currently have a static engine at the application level along with a dictionary of compiled scripts; then, per request, I create a new scope and execute the code within that scope...

Is that correct? thread safe? and as performant as it could be?

EDIT: regarding the bounty Please also answer the question I posed in reply to Jeff: Will a static instance of the engine cause sequential requests from different clients to wait in line to execute? if so I will probably need everything on a per-request basis.

2

There are 2 answers

3
Jeff Hardy On BEST ANSWER

A ScriptRuntime/ScriptEngine per application and a Scope per request is exactly how it should be done. Runtimes/Engine are thread-safe and Scopes are not.

0
Shiv Kumar On

Per request is the way to go unless all of your code is thread safe. You may get better performance using per application (per session implies you have th notion of "sesions" between you client and server), however the implication there is that all of your code in the "application" is thread safe.

So per-request is what you should use unless you know your code to be thread safe.

Note also that per application will be faster only if:

  1. In order to make things thread safe you've not blocking threads in any way.
  2. To a certain extent if the business layer/data layer are extremely "heavy" (take a lot of time to instantiate) then some performance benefit may be gained.