【问题标题】:How to host an IronPython engine in a separate AppDomain?如何在单独的 AppDomain 中托管 IronPython 引擎?
【发布时间】:2009-12-01 03:54:20
【问题描述】:

我正在使用以下代码在来自 c# 的单独“appDomain”上执行 IronPython 脚本。 (我使用这种方法来解决内存泄漏问题) 花费较少时间(少于 3 分钟)的脚本执行良好。

但是如果脚本需要更长的时间(超过 5 分钟)抛出异常说 -> System.Runtime.Remoting.RemotingException:对象'/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 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();
    }
  }
}

【问题讨论】:

  • 我没有真正的解决方案,但作为快速解决方案,我相信您可以通过设置 LifetimeServices.LeaseTime 来增加 AppDomain 的远程处理超时。我相信必须在 target AppDomain 中执行(虽然尚未测试!)。最简单的方法可能是让 Python 引擎在沙箱域中为您设置属性。

标签: c# ironpython embedding


【解决方案1】:

覆盖 InitializeLifetimeServices 并返回 null 将是正常的方法。我怀疑在你的情况下这是可能的。在 app.config 文件中包含<lifetime> element 是另一种方法。

【讨论】:

  • 2.0.x 中似乎缺少 InitializeLifetimeService 的覆盖,但 IronPython 2.6 中存在。
猜你喜欢
  • 2011-01-01
  • 1970-01-01
  • 2011-03-23
  • 2016-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多