次の方法で共有


Hosting IronPython made easier

With 2.0 Beta 5 coming out very soon, there is a new hosting helper class in there called IronPython.Hosting.Python . It has a few helper methods to create a ScriptRuntime or ScriptEngine and adds some Python-specific extension methods to ScriptRuntime and ScriptEngine.

Creating a ScriptRuntime the normal way now requires a LanguageSetup as Sesh explains here. The Python class has helpers called CreateRuntimeSetup and CreateLanguageSetup which return a pythonic ScriptRuntimeSetup and LanguageSetup  respectively. But if you are not interested in the configuration options of the DLR then you can simply use Python.CreateEngine/ Python.CreateRuntime.

 ScriptRuntime runtime = Python.CreateRuntime();
runtime.ExecuteFile("foo.py");

There are also three extension methods tacked on to ScriptRuntime and ScriptEngine called GetSysModule, GetBuiltinModule and GetClrModule which directly return a ScriptScope for these modules. You can do something like this for example:

 using IronPython.Hosting;

ScriptEngine engine = Python.CreateEngine();

ScriptScope sys = engine.GetSysModule();
var platform = sys.GetVariable("platform");
Console.WriteLine(platform);

ScriptScope builtins = engine.GetBuiltinModule();
var pow = builtins.GetVariable<Func<double, double,double>>("pow");
Console.WriteLine(pow(2,3));

ScriptScope clr = engine.GetClrModule();
var getPythonType = clr.GetVariable<Func<Type, PythonType>>("GetPythonType");            
Console.WriteLine(PythonType.Get__name__(getPythonType(typeof(string))));

This will then print out cli, 8 and str respectively.

Comments

  • Anonymous
    September 16, 2008
    PingBack from http://hoursfunnywallpaper.cn/?p=6454

  • Anonymous
    September 17, 2008
    DLR(Dynamic Language Runtime)是Silverlight中提供的一套非常强大的动态语言运行时。目前2.0 beta2中支持Python,Ruby和JSX。 利用DLR,你可以很方便的使用熟悉的动态语言编写Silverlight程序。

  • Anonymous
    September 22, 2008
    Thanks for the tips. In the beginning I wasn't "using IronPython.Hosting" so couldn't see the extention methods. Maybe you should add it to the example code.

  • Anonymous
    September 23, 2008
    Ahh.. sorry for the confustion. Updated the sample code.

  • Anonymous
    September 28, 2008
    ironpython2.0beta5已经发布,下载地址:http://www.codeplex.com/IronPython/Release/ProjectReleases.aspx?Rele...

  • Anonymous
    October 30, 2008
    IronPython use Dyanmic Assembly to generate IL codes. These assemblies will be loaded into the default AppDomain where the application runs. Unfortunately, there is no way to unload an Assembly without unload the entire AppDomain. This leads to the consistantly memory usage and finally cause memory leak. This is a 'mechanical damage' of not only IronPython but also the CLR. Sorry for my poor English :P

  • Anonymous
    October 31, 2008
    What you can do is use an option called LightWeightScopes. Setting an environment variable called DLR_LightWeightScopes will make IronPython generate scopes that gets garbage-collected. Instead of defining a type that holds globals, an array is used instead. I believe there might be a small perf impact but you wont lose memory. Also making your host run in a separate appdomain is also very simple. Python.CreateEngine or Python.CreateRuntime take an AppDomain as a param. Everything done then on the runtime will be in that appdomain.

  • Anonymous
    December 03, 2008
    Thanks for the information on how to easily host Ironpython.  I have followed your steps and I now have the ability to run Ironpython scripts within my c# application, very cool.   I was wondering how I could debug my hosted Ironpython scripts.  I have go in and set an option to debug when calling CreateRuntime.  This allows me to place a break point in my script and even step through the code.  The problem is I am unable to view the state of any variables.  Do you have any idea how I can view my variables in the script? Thanks!

  • Anonymous
    December 11, 2008
    The only way I've found to view variables thus far is to look at your "Locals" window and expand the following: $globalContext.GlobalScope.Dict.SymbolAttributes This will show you a list of all of the symbols (variables) currently set for your script. It's not really pretty, but it works in a pinch.

  • Anonymous
    December 22, 2008
    I'm afraid like Toji says there is no good way to do this.

  • Anonymous
    December 27, 2008
    Thanks for the response.  I will try out the suggestion and see if it works.  At least I have a break points and that is a step in the right direction.

  • Anonymous
    March 10, 2009
    Could someone please show me how to implement debugging ironpython scripts with a hosted ironpython engine. I have hosted IronPython in a vb.net application and got it to work. I want to know how to implement debugging within my vb.net application? thanks.