[IronPython] how to generate multiple concurrent scriptign engines?

Jeff Hardy jdhardy at gmail.com
Fri Apr 15 09:40:43 PDT 2011


Can one of your alter-egos file a bug so it doesn't get lost?

- Jeff

On Fri, Apr 15, 2011 at 10:36 AM, Dino Viehland <dinov at microsoft.com> wrote:

>  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.
>
>
>
> *From:* users-bounces at lists.ironpython.com [mailto:
> users-bounces at lists.ironpython.com] *On Behalf Of *Jeff Hardy
> *Sent:* Friday, April 15, 2011 7:29 AM
> *To:* surangika ranathunga
> *Cc:* Discussion of IronPython
>
> *Subject:* Re: [IronPython] how to generate multiple concurrent scriptign
> engines?
>
>
>
> 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.
>
>
>
> - Jeff
>
> On Thu, Apr 14, 2011 at 9:18 PM, surangika ranathunga <
> lady_radsu at yahoo.com> wrote:
>
> Hi,
> Below is the exception I receive:
> here, D:\PhD
> Work\Research\libomv\test\MonitorService\MonitorService\ExpectationMonitor.cs:line
> 88 refers to source.Execute(scope); in the below given code.
>
> System.Reflection.TargetInvocationException: Exception has been thrown by
> the target of an invocation. --->
> System.Reflection.TargetInvocationException: Exception has been thrown by
> the target of an invocation. ---> System.NullReferenceExce
> ption: Object reference not set to an instance of an object.
>    at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue
> value, Boolean add)
>    at System.Collections.Generic.Dictionary`2.set_Item(TKey key, TValue
> value)
>    at IronPython.Runtime.PythonContext.GetEqualSite(Type type)
>    at IronPython.Runtime.CommonDictionaryStorage.UpdateHelperFunctions(Type
> t, Object key)
>    at IronPython.Runtime.CommonDictionaryStorage.AddNoLock(Object key,
> Object value)
>    at IronPython.Runtime.CommonDictionaryStorage.Add(Object key, Object
> value)
>    at IronPython.Runtime.PythonDictionary.SetItem(Object key, Object value)
>    at IronPython.Runtime.PythonDictionary.set_Item(Object key, Object
> value)
>    at IronPython.Modules.PythonCopyReg.PerformModuleReload(PythonContext
> context, PythonDictionary dict)
>    --- End of inner exception stack trace ---
>    at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo
> method, Object target, Object[] arguments, SignatureStruct& sig,
> MethodAttributes methodAttributes, RuntimeType typeOwner)
>    at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo
> method, Object target, Object[] arguments, Signature sig, MethodAttributes
> methodAttributes, RuntimeType typeOwner)
>    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
> invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean
> skipVisibilityChecks)
>    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
> invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
>    at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
>    at IronPython.Runtime.PythonContext.CreateBuiltinModule(String
> moduleName, Type type, ModuleOptions options)
>    at IronPython.Runtime.PythonContext.CreateBuiltinModule(String
> moduleName, Type type)
>    at IronPython.Runtime.PythonContext.CreateBuiltinModule(String name)
>    at IronPython.Runtime.PythonContext.GetBuiltinModule(String name)
>    at IronPython.Runtime.Importer.ImportBuiltin(CodeContext context, String
> name)
>    at IronPython.Modules.PythonCopyReg.EnsureModuleInitialized(CodeContext
> context)
>    at IronPython.Modules.PythonCopyReg.GetDispatchTable(CodeContext
> context)
>    at IronPython.Modules.PythonRegex.PerformModuleReload(PythonContext
> context,PythonDictionary dict)
>    --- End of inner exception stack trace ---
>    at
> Microsoft.Scripting.Actions.Calls.MethodCandidate.Caller.Call(Object[] args,
> Boolean& shouldOptimize)
>    at
> IronPython.Runtime.Types.BuiltinFunction.BuiltinFunctionCaller`5.Call4(CallSite
> site, CodeContext context, TFuncType func, T0 arg0, T1 arg1, T2 arg2, T3
> arg3)
>    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)
>    at IronPython.Runtime.Importer.Import(CodeContext context, String
> fullName, PythonTuple from, Int32 level)
>    at IronPython.Runtime.Operations.PythonOps.ImportTop(CodeContext
> context, String fullName, Int32 level)
>    at
> Microsoft.Scripting.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame
> frame)
>    at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame
> frame)
>    at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0,
> T1 arg1)
>    at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
>    at IronPython.Compiler.PythonScriptCode.Run(Scope scope)
>    at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
>    at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)
>    at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink
> errorSink)
>    at Microsoft.Scripting.SourceUnit.Execute(Scope scope)
>    at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
>    at MonitorService.ExpectationMonitor.Initialize(String rule) in D:\PhD
> Work\Research\libomv\test\MonitorService\MonitorService\ExpectationMonitor.cs:line
> 88
>
> --- On *Thu, 14/4/11, Jeff Hardy <jdhardy at gmail.com>* wrote:
>
>
> From: Jeff Hardy <jdhardy at gmail.com>
> Subject: Re: [IronPython] how to generate multiple concurrent scriptign
> engines?
> To: "Discussion of IronPython" <users at lists.ironpython.com>
> Cc: "surangika ranathunga" <lady_radsu at yahoo.com>
> Received: Thursday, 14 April, 2011, 7:35 AM
>
>
>
> As long as you have a ScriptScope per thread, you should be able to
> share the ScriptEngine & Runtime between threads.
>
> What is the exception that you are getting? If it's a GUI program,
> remember that UI elements can only be manipulated from the main
> thread.
>
> - Jeff
>
> On Thu, Apr 14, 2011 at 1:02 AM, surangika ranathunga
> <lady_radsu at yahoo.com <http://mc/compose?to=lady_radsu@yahoo.com>> wrote:
> >
> > Hi,
> > I am using Ironpython to connect to a legacy python code from C#.
> > I am not fully familiar with Ironpython, but managed to get it working
> for a single-threaded application.
> > This is how I implemented this:
> >
> >                 ScriptEngine engine = Python.CreateEngine();
> >                 ScriptRuntime runtime = engine.Runtime;
> >                 ScriptScope scope = runtime.CreateScope();
> >                 ops = engine.Operations;
> >                 ScriptSource source =
> engine.CreateScriptSourceFromFile("ironpython.py");
> >                 source.Execute(scope);
> >                 string class_param = rule;
> >                 object[] parameters = new object[1];
> >                 parameters[0] = class_param;
> >                 object klass = scope.GetVariable("model_checker");
> >                 object instance = ops.Invoke(klass, parameters);
> >                 method = ops.GetMember(instance, "check_model");
> >
> > 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.
> > 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.
> >
> > Is there anything specific that I should do to generate multiple
> scripting engines running in separate threads without interfering with each
> other?
> >
> > any ideas greatly appreciated
> >
> > _______________________________________________
> > Users mailing list
> > Users at lists.ironpython.com<http://mc/compose?to=Users@lists.ironpython.com>
> > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> >
>
>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ironpython.com/pipermail/users-ironpython.com/attachments/20110415/23b083da/attachment-0001.htm>


More information about the Users mailing list