[IronPython] Multi threaded engine
ncichris at gmail.com
Mon Jun 7 15:14:34 CEST 2010
I am investigating different possibilities regarding multithreading and Iron
Python and I would appreciate some pointers.
What I am currently doing is I allow the user to enter some pseudocode that
will become some business logic running in a high traffic application
The main properties of this project is that the user has to be able to
change these rules ad hoc and then ask then server to load the rules. The
server will load the rules convert these to IronPython, compile the code and
then for each message run the compile code against some parameters retrieved
from the message.
Since there are hundreds of messages coming in the server per second to make
this work with multiple threads I defined the ScriptEngine and the
CompiledCode as [ThreadStatic]. Therefore every time I have to use the
engine and the engine is null I will create a new engine (one per thread).
Then I will retrieve all the rules from the DB convert the rules to Python
code, and compile the code using engine.CreateScriptSourceFromString(),
Then to run the rules on the message I create a
Microsoft.Scripting.ScopeStorage, call .SetValue to set each parameter that
I want to be used by the script, then use the engine to CreateScope and use
the compiled code and that Scope to execute the script.
Everything seems to work fine but I am wondering if this is the proper way
to do this. I have not seen anywhere any references to multithreaded uses of
IronPython and this creates some uncertainties to me. What I would like to
know is if there is a more elegant and faster way to do this, since the
rules start getting bigger and bigger and this slows down both compilation
and execution of the scripts.
I am thinking that it could be better if the ScriptEngine and CompiledCode
are thread safe, and if I create one static engine an one static
CompliledCode then this will minimize the compile time since the rules will
have to be compiled one time and then reused for all threads.
Anyway any tips on running dynamic IronPython code are welcome.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Ironpython-users