<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
 /* Font Definitions */
 @font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page Section1
        {size:612.0pt 792.0pt;
        margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.Section1
        {page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext="edit">
  <o:idmap v:ext="edit" data="1" />
 </o:shapelayout></xml><![endif]-->
</head>

<body lang=EL link=blue vlink=purple>

<div class=Section1>

<p class=MsoNormal><span lang=EN-US>I am investigating different possibilities
regarding multithreading and Iron Python and I would appreciate some pointers.<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US>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 server. <o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US>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.<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span lang=EN-US>Since there are hundreds of messages coming
in the server per second to make this work with multiple threads I&nbsp;
defined the ScriptEngine and the CompiledCode as &nbsp;[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(),
source.Compile.<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US>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.<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span lang=EN-US>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&nbsp; 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. <o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span lang=EN-US>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. <o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span lang=EN-US>Anyway any tips on running dynamic
IronPython code are welcome.<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p>

</div>

</body>

</html>