开发者

How to host an IronPython engine in a separate AppDomain?

I am using the below code to execute the IronPython Script on separate "appDomain" from c#. (I used this approach to resolve memory leakage issue) The scripts which take a lesser time (less than 3 mins), executes fine.

But if the script which takes a longer time (more than 5mins) throws an exception saying -> System.Runtime.Remoting.RemotingException: Object '/011b230e_2f28_4caa_8bbc_92fabb63b311/vhpajnwe48ogwedf6zwikqow_4.rem'

using System;
using Microsoft.Scripting;

namespace PythonHostSamle
{
  class Program
  {
    static void Main(string[] args)
    {
        AppDomain sandbox = AppDomain.CreateDomain("sandbox");
        var engine = IronPython.Hosting.Python.CreateEngine(sandbox);
        var searchPaths = engine.GetSearchPaths();
        searchPaths.Add(@"C:\Python25\Lib");
        searchPaths.Add(@"C:\RevitPythonShell");
        engine.SetSearchPaths(searchPaths);

        ScriptScope scope = engine.ExecuteFile("C:\Python25\Test.py")
        // Script takes morethan 5mins开发者_如何学Go to execute(sleep in the script)

        ObjectHandle oh = scope.GetVariableHandle("GlobalVariableName")
        // System throws following exception              
        //System.Runtime.Remoting.RemotingException:
        // Object '/011b230e_2f28_4caa_8bbc_92fabb63b311/vhpajnwe48ogwedf6zwikqow_4.rem'

        Console.ReadKey();
    }
  }
}


Overriding InitializeLifetimeServices and returning null would be the normal approach. I doubt that's possible in your case. Including the <lifetime> element in the app.config file is another approach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜