[IronPython] Query regarding Proxy Object lease period

Dino Viehland dinov at microsoft.com
Wed Aug 11 18:56:43 CEST 2010

What version of IronPython are you using?  When I try this against 2.6.1 ScriptScope is "properly" returning null.    I would recommend seeing how the lifetime issue stacks up w/ everything returning null.    I think long term we need to provide an API that lets you provide the sponsors for hosting API objects but until we do that leasing your own objects may be futile unless there a whole lot of them in comparison to the hosting API objects.  I actually  haven't done the leasing stuff myself either so I'm not sure I can quickly cook up an example.

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of mohammad mustaq
Sent: Wednesday, August 11, 2010 7:30 AM
To: Discussion of IronPython
Subject: Re: [IronPython] Query regarding Proxy Object lease period

I did override "InitializeLifetimeService" in MbrBase to return null. But in my example the "scope" object dies at first instance, so how would overriding "InitializeLifetimeService" in MbrBase help. Could you please modify my code to illustrate the right usage of ISponsor on the client side.

My application will create objects periodically in the appdomain and execute methods implemented in IronPython. These methods may take around 20 minutes to execute. The whole application may run for a day or more. So I need to take this into consideration while implementing the lifetime service.

I needed to confirm one thing. Do i need to provide the lease period for all objects created in the Appdomain. As per the code given below do i need to provide the lease period for "code","scope","mbr","script", "ops" etc.
Or is it only based on how long I expect the object to be alive.

I am new to this concept of Appdomain hence my questions may sound silly.
On Tue, Aug 10, 2010 at 10:15 PM, Dino Viehland <dinov at microsoft.com<mailto:dinov at microsoft.com>> wrote:
The trick to keeping an object alive isn't to call InitializeLifetimeService yourself but instead to return an appropriate lease.  For example if you overrode InitializeLifetimeService in MbrBase to always return null your object will be kept alive forever.  That will work fine as long as the app domain isn't living for an extremely long time or that you have a limited number of these MBROs that you're creating in the app domain.

We still have this problem to solve in the DLR as well where we're still returning null.  I think  the correct solution is that we need to use ISponsor's (ClientSPonsor provides an implementation of this) on the client side and then call ILease.Register so the object can call back to your sponser.

From: users-bounces at lists.ironpython.com<mailto:users-bounces at lists.ironpython.com> [mailto:users-bounces at lists.ironpython.com<mailto:users-bounces at lists.ironpython.com>] On Behalf Of mohammad mustaq
Sent: Tuesday, August 10, 2010 6:43 AM
To: Discussion of IronPython
Subject: [IronPython] Query regarding Proxy Object lease period


I posted this last week and I did not get a response.Hence posting it again.

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.


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);

        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<http://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);


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

    public void DoIt()
        Console.WriteLine("Did it {0}", AppDomain.CurrentDomain.Id<http://AppDomain.CurrentDomain.Id>);

Users mailing list
Users at lists.ironpython.com<mailto:Users at lists.ironpython.com>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100811/9bfa7f37/attachment.html>

More information about the Ironpython-users mailing list