[IronPython] Query regarding Object lease period

mohammad mustaq mustaq2001 at gmail.com
Tue Aug 3 16:31:46 CEST 2010


Hi,

I have an application that creates a python engine in a new appdomain. If I
do not specify a lease period I see that the objects are disconnected when I
leave it idle for a period of 5 minutes. I used "InitializeLifetimeService"
to keep the object alive forever but it did not work. The "dlr-spec-hosting"
document mentions the following in page 93 under "Current Issues" section :
"Currently if you do not use a host for 15 min, it dies, and you lose your
data. We've added InitializeLifetimeService on objects to cause them to stay
alive forever, but we need to think through the design and the host controls
here". Currently I renew the object using "ILease". I do not know if this is
the right thing to do. Could you please suggest me the right way to deal
with this issue. I have provided the code below for your reference.

Thanks,
Mustaq

using System;
using Microsoft.Scripting;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;

class Foo
{
    public static void Main(string[] args)
    {
        AppDomain ad = AppDomain.CreateDomain("foo");
        ScriptEngine engine = Python.CreateEngine(ad);
        engine.Runtime.LoadAssembly(typeof(MbrBase).Assembly);

        ScriptSource code = engine.CreateScriptSourceFromString(@"
import MbrBase
class C(MbrBase):
    def Test_C(self, log):
        print 0
a = C()
", SourceCodeKind.Statements);

        ScriptScope scope = engine.CreateScope();
        ILease lifeTime = (ILease)scope.InitializeLifetimeService(); // Is
this supposed to keep the object alive forever.

        lifeTime.Renew(TimeSpan.FromDays(1)); // Provided a lease for one
day.
        code.Execute(scope);  // If the above lease is not mentioned then
execution fails on this line after being inactive for 5 minutes.

        Console.WriteLine("Trying to do it... {0}",
AppDomain.CurrentDomain.Id);
        MbrBase mbr = (MbrBase)scope.GetVariable("a");

        string isSubClassCode = String.Format("issubclass({0},{1})", "C",
"MbrBase");
        ScriptSource script =
engine.CreateScriptSourceFromString(isSubClassCode,
SourceCodeKind.Expression);
        bool result = (bool)script.Execute(scope);

        if (result == true)
        {
            ObjectOperations ops = engine.Operations;
            ObjectHandle subClass = scope.GetVariableHandle("C");    // get
back a handle
            ObjectHandle instance = ops.Call(subClass, new
ObjectHandle[0]);  // invoke the handle to create an instance
            mbr = instance.Unwrap() as MbrBase;  // now we know we have an
MBR and we can unwrap it to our local side

            ObjectHandle temp = ops.GetMember(instance, "Test_C");
            object log = null;
            ops.Call(temp, log);
        }

        mbr.DoItVirtually();
        mbr.DoIt();
        Console.ReadKey();
    }
}

public class MbrBase : MarshalByRefObject
{
    public virtual void DoItVirtually()
    {
        Console.WriteLine("Did it virtually {0}", AppDomain.CurrentDomain.Id
);
    }

    public void DoIt()
    {
        Console.WriteLine("Did it {0}", AppDomain.CurrentDomain.Id);
    }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100803/37968d63/attachment.html>


More information about the Ironpython-users mailing list