我无法找到最新的语法,可以将Ironpython ScriptEngine放入自己的AppDomain in C#中。
到目前为止,我看到的唯一语法是像这样创建ScriptEngine:
AppDomain sandbox = AppDomain.CreateDomain("sandbox");
ScriptEngine engine = Python.CreateEngine( sandbox );这是行不通的,至少在IPY 2.7.7中不起作用,因此出现了以下例外情况:
SerializationException: Could not find type 'System.Collections.Generic.IList`1[[Microsoft.Scripting.Hosting.LanguageSetup, Microsoft.Scripting, Version=1.1.2.22, Culture=neutral, PublicKeyToken=7f709c5b713576e1]]'.
(wrapper xdomain-invoke) System.AppDomain:CreateInstanceAndUnwrap (string,string,bool,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo,object[],System.Security.Policy.Evidence)
(wrapper remoting-invoke-with-check) System.AppDomain:CreateInstanceAndUnwrap (string,string,bool,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo,object[],System.Security.Policy.Evidence)
Microsoft.Scripting.Hosting.ScriptRuntime.CreateRemote (System.AppDomain domain, Microsoft.Scripting.Hosting.ScriptRuntimeSetup setup)
IronPython.Hosting.Python.CreateRuntime (System.AppDomain domain)
IronPython.Hosting.Python.CreateEngine (System.AppDomain domain) 我还试图给出完整的参数集,正如我所理解的:http://answers.unity3d.com/questions/242901/problem-with-appdomainspermissionset-ironpython-in.html
但是,由于使用AppDomain作为参数创建Scriptengine的语法在2.7.7中被废弃或不可用,因此也会导致相同的错误。
此外,我还需要添加诸如"FullFrames“等Ironpython选项的字典,这正是Python.CreateEngine所期望的。
我需要将Ironpython放入它自己的AppDomain中,因为似乎存在某种类型的内存泄漏,导致在几个程序集加载后在Unity崩溃,或引擎的多个实例化(我执行空出并关闭运行时,以及当主例程在IPY中完成时垃圾收集)
以下是我目前正在做的工作:
Dictionary<string, object> options = new Dictionary<string, object>();
options["Frames"] = true;
options["FullFrames"] = true;
options["LightweightScopes"] = true;
options["ShowClrExceptions"] = true;
options["ExceptionDetail"] = true;
AppDomain sandbox = AppDomain.CreateDomain("sandbox");
// Don't know where to go from here to get my engine into the AppDomain.
engine = Python.CreateEngine(options);
scope = engine.CreateScope();
var paths = engine.GetSearchPaths();
paths.Add(@"C:\Python\IronPython2.7.7\Lib");
paths.Add(@"C:\Python\IronPython2.7.7\Lib\site-packages");
engine.SetSearchPaths(paths);
scope.SetVariable("test", "12345");
try
{
source.Execute(scope);
}
catch (SystemExitException e)
{
Debug.Log("IronPython exited with the following code ( " + e + " )");
}
catch (Exception e)
{
ExceptionOperations eo = engine.GetService<ExceptionOperations>();
string error = eo.FormatException(e);
Debug.Log("<color=red>IPYError : </color>" + error);
}
engine.Runtime.Shutdown();
engine = null;
runtime = null;
source = null;
scope = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();发布于 2017-06-16 12:01:25
问题是,必须首先将Microsoft.Scripting.Hosting的程序集以及它的所有依赖程序集(如MSCorLib.dll )加载到应用程序域中。因为IronPython ScriptEngine程序集应该在它自己的AppDomain中,所以它不再能够访问诸如‘System.Collection.Generic.IList’这样的程序集。一旦我这么做了,这句话就奏效了:
engine = Python.CreateEngine( sandbox, options);此外,它还修复了包含IronPython内存泄漏的United崩溃。缺点是,您将无法使用IronPython直接操作编辑器,缺少一些花哨的权限/热交换等等。
至少不再有死机了,这是天赐的。
https://stackoverflow.com/questions/44556922
复制相似问题