Can one of your alter-egos file a bug so it doesn&#39;t get lost?<div><br></div><div>- Jeff<br><br><div class="gmail_quote">On Fri, Apr 15, 2011 at 10:36 AM, Dino Viehland <span dir="ltr">&lt;<a href="mailto:dinov@microsoft.com">dinov@microsoft.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">





<div lang="EN-US" link="blue" vlink="purple">
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D">It might come from CopyReg but the bug is definitely in GetEqualSite – we just need a lock(_equalSites) { … } around the TryGetValue and creation of the equal
 site.</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D"> </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">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></p><div><div></div><div class="h5"><br>
<b>Subject:</b> Re: [IronPython] how to generate multiple concurrent scriptign engines?</div></div><p></p>
</div>
</div><div><div></div><div class="h5">
<p class="MsoNormal"> </p>
<p class="MsoNormal">Any chance you can share what the source is? The exception comes from PythonCopyReg&#39;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.</p>
<div>
<p class="MsoNormal"> </p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">- Jeff</p>
<div>
<p class="MsoNormal">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:</p>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" style="padding:0in 0in 0in 0in">
<p class="MsoNormal">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>
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)<br>
   at System.Collections.Generic.Dictionary`2.set_Item(TKey key, TValue value)<br>
   at IronPython.Runtime.PythonContext.GetEqualSite(Type type)<br>
   at IronPython.Runtime.CommonDictionaryStorage.UpdateHelperFunctions(Type t, Object key)<br>
   at IronPython.Runtime.CommonDictionaryStorage.AddNoLock(Object key, Object value)<br>
   at IronPython.Runtime.CommonDictionaryStorage.Add(Object key, Object value)<br>
   at IronPython.Runtime.PythonDictionary.SetItem(Object key, Object value)<br>
   at IronPython.Runtime.PythonDictionary.set_Item(Object key, Object value)<br>
   at IronPython.Modules.PythonCopyReg.PerformModuleReload(PythonContext context, PythonDictionary dict)<br>
   --- End of inner exception stack trace ---<br>
   at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct&amp; sig, MethodAttributes methodAttributes, RuntimeType typeOwner)<br>
   at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)<br>
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)<br>
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)<br>
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)<br>
   at IronPython.Runtime.PythonContext.CreateBuiltinModule(String moduleName, Type type, ModuleOptions options)<br>
   at IronPython.Runtime.PythonContext.CreateBuiltinModule(String moduleName, Type type)<br>
   at IronPython.Runtime.PythonContext.CreateBuiltinModule(String name)<br>
   at IronPython.Runtime.PythonContext.GetBuiltinModule(String name)<br>
   at IronPython.Runtime.Importer.ImportBuiltin(CodeContext context, String name)<br>
   at IronPython.Modules.PythonCopyReg.EnsureModuleInitialized(CodeContext context)<br>
   at IronPython.Modules.PythonCopyReg.GetDispatchTable(CodeContext context)<br>
   at IronPython.Modules.PythonRegex.PerformModuleReload(PythonContext context,PythonDictionary dict)<br>
   --- End of inner exception stack trace ---<br>
   at Microsoft.Scripting.Actions.Calls.MethodCandidate.Caller.Call(Object[] args, Boolean&amp; shouldOptimize)<br>
   at IronPython.Runtime.Types.BuiltinFunction.BuiltinFunctionCaller`5.Call4(CallSite site, CodeContext context, TFuncType func, T0 arg0, T1 arg1, T2 arg2, T3 arg3)<br>
   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>
   at IronPython.Runtime.Importer.Import(CodeContext context, String fullName, PythonTuple from, Int32 level)<br>
   at IronPython.Runtime.Operations.PythonOps.ImportTop(CodeContext context, String fullName, Int32 level)<br>
   at Microsoft.Scripting.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame frame)<br>
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)<br>
   at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)<br>
   at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)<br>
   at IronPython.Compiler.PythonScriptCode.Run(Scope scope)<br>
   at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)<br>
   at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)<br>
   at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)<br>
   at Microsoft.Scripting.SourceUnit.Execute(Scope scope)<br>
   at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)<br>
   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:</p>
<p class="MsoNormal"><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</p>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt"> </p>
<div>
<p class="MsoNormal">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&#39;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;                 ScriptEngine engine = Python.CreateEngine();<br>
&gt;                 ScriptRuntime runtime = engine.Runtime;<br>
&gt;                 ScriptScope scope = runtime.CreateScope();<br>
&gt;                 ops = engine.Operations;<br>
&gt;                 ScriptSource source = engine.CreateScriptSourceFromFile(&quot;ironpython.py&quot;);<br>
&gt;                 source.Execute(scope);<br>
&gt;                 string class_param = rule;<br>
&gt;                 object[] parameters = new object[1];<br>
&gt;                 parameters[0] = class_param;<br>
&gt;                 object klass = scope.GetVariable(&quot;model_checker&quot;);<br>
&gt;                 object instance = ops.Invoke(klass, parameters);<br>
&gt;                 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;</p>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="MsoNormal"> </p>
</div>
</div></div></div>
</div>
</div>

<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><br>
<br></blockquote></div><br></div>