<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 14 (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;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","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;}
p
        {mso-style-priority:99;
        mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
        {mso-style-priority:99;
        mso-style-link:"Balloon Text Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:8.0pt;
        font-family:"Tahoma","sans-serif";}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.BalloonTextChar
        {mso-style-name:"Balloon Text Char";
        mso-style-priority:99;
        mso-style-link:"Balloon Text";
        font-family:"Tahoma","sans-serif";}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></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="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D">Filed as
<a href="http://ironpython.codeplex.com/workitem/30551">http://ironpython.codeplex.com/workitem/30551</a>
<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D"><o:p>&nbsp;</o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">From:</span></b><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;"> Jeff Hardy [mailto:jdhardy@gmail.com]
<br>
<b>Sent:</b> Friday, April 15, 2011 9:41 AM<br>
<b>To:</b> Discussion of IronPython<br>
<b>Cc:</b> Dino Viehland; surangika ranathunga<br>
<b>Subject:</b> Re: [IronPython] how to generate multiple concurrent scriptign engines?<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">Can one of your alter-egos file a bug so it doesn't get lost?<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">- Jeff<o:p></o:p></p>
<div>
<p class="MsoNormal">On Fri, Apr 15, 2011 at 10:36 AM, Dino Viehland &lt;<a href="mailto:dinov@microsoft.com">dinov@microsoft.com</a>&gt; wrote:<o:p></o:p></p>
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:#1F497D">It might come from CopyReg but the bug is definitely in GetEqualSite &#8211; we just need a lock(_equalSites) { &#8230; } around the TryGetValue
 and creation of the equal site.</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:#1F497D">&nbsp;</span><o:p></o:p></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><b><span style="font-size:10.0pt">From:</span></b><span style="font-size:10.0pt">
<a href="mailto:users-bounces@lists.ironpython.com" target="_blank">users-bounces@lists.ironpython.com</a> [mailto:<a href="mailto:users-bounces@lists.ironpython.com" target="_blank">users-bounces@lists.ironpython.com</a>]
<b>On Behalf Of </b>Jeff Hardy<br>
<b>Sent:</b> Friday, April 15, 2011 7:29 AM<br>
<b>To:</b> surangika ranathunga<br>
<b>Cc:</b> Discussion of IronPython</span><o:p></o:p></p>
<div>
<div>
<p class="MsoNormal"><br>
<b>Subject:</b> Re: [IronPython] how to generate multiple concurrent scriptign engines?<o:p></o:p></p>
</div>
</div>
</div>
</div>
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">&nbsp;<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Any chance you can share what the source is? The exception comes from PythonCopyReg's initialization, which means we might have a bug (it might not be thread-safe, or something
 else). If you could create a minimal sample that has the issue that would be even better.<o:p></o:p></p>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">&nbsp;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;margin-bottom:12.0pt">- Jeff<o:p></o:p></p>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">On Thu, Apr 14, 2011 at 9:18 PM, surangika ranathunga &lt;<a href="mailto:lady_radsu@yahoo.com" target="_blank">lady_radsu@yahoo.com</a>&gt; wrote:<o:p></o:p></p>
<table class="MsoNormalTable" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" style="padding:0in 0in 0in 0in">
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Hi,<br>
Below is the exception I receive:<br>
here, D:\PhD Work\Research\libomv\test\MonitorService\MonitorService\ExpectationMonitor.cs:line 88 refers to source.Execute(scope); in the below given code.<br>
<br>
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---&gt; System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---&gt; System.NullReferenceExce<br>
ption: Object reference not set to an instance of an object.<br>
&nbsp;&nbsp; at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)<br>
&nbsp;&nbsp; at System.Collections.Generic.Dictionary`2.set_Item(TKey key, TValue value)<br>
&nbsp;&nbsp; at IronPython.Runtime.PythonContext.GetEqualSite(Type type)<br>
&nbsp;&nbsp; at IronPython.Runtime.CommonDictionaryStorage.UpdateHelperFunctions(Type t, Object key)<br>
&nbsp;&nbsp; at IronPython.Runtime.CommonDictionaryStorage.AddNoLock(Object key, Object value)<br>
&nbsp;&nbsp; at IronPython.Runtime.CommonDictionaryStorage.Add(Object key, Object value)<br>
&nbsp;&nbsp; at IronPython.Runtime.PythonDictionary.SetItem(Object key, Object value)<br>
&nbsp;&nbsp; at IronPython.Runtime.PythonDictionary.set_Item(Object key, Object value)<br>
&nbsp;&nbsp; at IronPython.Modules.PythonCopyReg.PerformModuleReload(PythonContext context, PythonDictionary dict)<br>
&nbsp;&nbsp; --- End of inner exception stack trace ---<br>
&nbsp;&nbsp; at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct&amp; sig, MethodAttributes methodAttributes, RuntimeType typeOwner)<br>
&nbsp;&nbsp; at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)<br>
&nbsp;&nbsp; at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)<br>
&nbsp;&nbsp; at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)<br>
&nbsp;&nbsp; at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)<br>
&nbsp;&nbsp; at IronPython.Runtime.PythonContext.CreateBuiltinModule(String moduleName, Type type, ModuleOptions options)<br>
&nbsp;&nbsp; at IronPython.Runtime.PythonContext.CreateBuiltinModule(String moduleName, Type type)<br>
&nbsp;&nbsp; at IronPython.Runtime.PythonContext.CreateBuiltinModule(String name)<br>
&nbsp;&nbsp; at IronPython.Runtime.PythonContext.GetBuiltinModule(String name)<br>
&nbsp;&nbsp; at IronPython.Runtime.Importer.ImportBuiltin(CodeContext context, String name)<br>
&nbsp;&nbsp; at IronPython.Modules.PythonCopyReg.EnsureModuleInitialized(CodeContext context)<br>
&nbsp;&nbsp; at IronPython.Modules.PythonCopyReg.GetDispatchTable(CodeContext context)<br>
&nbsp;&nbsp; at IronPython.Modules.PythonRegex.PerformModuleReload(PythonContext context,PythonDictionary dict)<br>
&nbsp;&nbsp; --- End of inner exception stack trace ---<br>
&nbsp;&nbsp; at Microsoft.Scripting.Actions.Calls.MethodCandidate.Caller.Call(Object[] args, Boolean&amp; shouldOptimize)<br>
&nbsp;&nbsp; at IronPython.Runtime.Types.BuiltinFunction.BuiltinFunctionCaller`5.Call4(CallSite site, CodeContext context, TFuncType func, T0 arg0, T1 arg1, T2 arg2, T3 arg3)<br>
&nbsp;&nbsp; at System.Dynamic.UpdateDelegates.UpdateAndExecute6[T0,T1,T2,T3,T4,T5,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)<br>
&nbsp;&nbsp; at IronPython.Runtime.Importer.Import(CodeContext context, String fullName, PythonTuple from, Int32 level)<br>
&nbsp;&nbsp; at IronPython.Runtime.Operations.PythonOps.ImportTop(CodeContext context, String fullName, Int32 level)<br>
&nbsp;&nbsp; at Microsoft.Scripting.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame frame)<br>
&nbsp;&nbsp; at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)<br>
&nbsp;&nbsp; at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)<br>
&nbsp;&nbsp; at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)<br>
&nbsp;&nbsp; at IronPython.Compiler.PythonScriptCode.Run(Scope scope)<br>
&nbsp;&nbsp; at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)<br>
&nbsp;&nbsp; at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)<br>
&nbsp;&nbsp; at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)<br>
&nbsp;&nbsp; at Microsoft.Scripting.SourceUnit.Execute(Scope scope)<br>
&nbsp;&nbsp; at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)<br>
&nbsp;&nbsp; at MonitorService.ExpectationMonitor.Initialize(String rule) in D:\PhD Work\Research\libomv\test\MonitorService\MonitorService\ExpectationMonitor.cs:line 88<br>
<br>
--- On <b>Thu, 14/4/11, Jeff Hardy <i>&lt;<a href="mailto:jdhardy@gmail.com" target="_blank">jdhardy@gmail.com</a>&gt;</i></b> wrote:<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><br>
From: Jeff Hardy &lt;<a href="mailto:jdhardy@gmail.com" target="_blank">jdhardy@gmail.com</a>&gt;<br>
Subject: Re: [IronPython] how to generate multiple concurrent scriptign engines?<br>
To: &quot;Discussion of IronPython&quot; &lt;<a href="mailto:users@lists.ironpython.com" target="_blank">users@lists.ironpython.com</a>&gt;<br>
Cc: &quot;surangika ranathunga&quot; &lt;<a href="mailto:lady_radsu@yahoo.com" target="_blank">lady_radsu@yahoo.com</a>&gt;<br>
Received: Thursday, 14 April, 2011, 7:35 AM<o:p></o:p></p>
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;margin-bottom:12.0pt">&nbsp;<o:p></o:p></p>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">As long as you have a ScriptScope per thread, you should be able to<br>
share the ScriptEngine &amp; Runtime between threads.<br>
<br>
What is the exception that you are getting? If it's a GUI program,<br>
remember that UI elements can only be manipulated from the main<br>
thread.<br>
<br>
- Jeff<br>
<br>
On Thu, Apr 14, 2011 at 1:02 AM, surangika ranathunga<br>
&lt;<a href="http://mc/compose?to=lady_radsu@yahoo.com" target="_blank">lady_radsu@yahoo.com</a>&gt; wrote:<br>
&gt;<br>
&gt; Hi,<br>
&gt; I am using Ironpython to connect to a legacy python code from C#.<br>
&gt; I am not fully familiar with Ironpython, but managed to get it working for a single-threaded application.<br>
&gt; This is how I implemented this:<br>
&gt;<br>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ScriptEngine engine = Python.CreateEngine();<br>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ScriptRuntime runtime = engine.Runtime;<br>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ScriptScope scope = runtime.CreateScope();<br>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ops = engine.Operations;<br>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ScriptSource source = engine.CreateScriptSourceFromFile(&quot;ironpython.py&quot;);<br>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; source.Execute(scope);<br>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string class_param = rule;<br>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; object[] parameters = new object[1];<br>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; parameters[0] = class_param;<br>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; object klass = scope.GetVariable(&quot;model_checker&quot;);<br>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; object instance = ops.Invoke(klass, parameters);<br>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; method = ops.GetMember(instance, &quot;check_model&quot;);<br>
&gt;<br>
&gt; Now I want to make my application multi-threaded, and I want to have concurrently running scripting engines. Essentially, these engines should access the same underlying python code, and hence these scripting engines are identical to each other.<br>
&gt; Without any change, the above code works fine for most of the time for a multi-threaded application. However, there is an exception that appears from time to time, and I suspect that it is due to a concurrency issue.<br>
&gt;<br>
&gt; Is there anything specific that I should do to generate multiple scripting engines running in separate threads without interfering with each other?<br>
&gt;<br>
&gt; any ideas greatly appreciated<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; Users mailing list<br>
&gt; <a href="http://mc/compose?to=Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a><br>
&gt; <a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
&gt;<o:p></o:p></p>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">&nbsp;<o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><br>
_______________________________________________<br>
Users mailing list<br>
<a href="mailto:Users@lists.ironpython.com">Users@lists.ironpython.com</a><br>
<a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
</div>
</div>
</body>
</html>