If you import by executing the text &quot;import &lt;modname&gt;&quot; against the ScriptEngine instead of using the import API, you will avoid this particular incarnation of the bug.<br><br><div class="gmail_quote">On Thu, Apr 30, 2009 at 10:25 AM, Lepisto, Stephen P <span dir="ltr">&lt;<a href="mailto:stephen.p.lepisto@intel.com">stephen.p.lepisto@intel.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">I voted for fixing the bug.  In the meantime, I will be putting on hold the use of IronPython as an embedded language in my project until this is fixed or IronPython 2.6 is released (if I can convince my manager and team it&#39;s a good idea to move to python 2.6 but it will affect a lot of people).<br>

<br>
Thank you for the prompt responses!<br>
<br>
<br>
-----Original Message-----<br>
From: <a href="mailto:users-bounces@lists.ironpython.com">users-bounces@lists.ironpython.com</a> [mailto:<a href="mailto:users-bounces@lists.ironpython.com">users-bounces@lists.ironpython.com</a>] On Behalf Of Dino Viehland<br>

Sent: Thursday, April 30, 2009 10:23 AM<br>
To: Discussion of IronPython<br>
Subject: Re: [IronPython] Question on Multiple Discrete IronPython sessions in a single process<br>
<br>
Looks like our threads crossed.  Yep, I was using the current 2.6 branch.<br>
<br>
I opened a bug to fix this in 2.0.2 (<a href="http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=22239" target="_blank">http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=22239</a>).<br>
<br>
Thanks for reporting this - this is a very bad bug.<br>
<br>
&gt; -----Original Message-----<br>
&gt; From: <a href="mailto:users-bounces@lists.ironpython.com">users-bounces@lists.ironpython.com</a> [mailto:<a href="mailto:users-">users-</a><br>
&gt; <a href="mailto:bounces@lists.ironpython.com">bounces@lists.ironpython.com</a>] On Behalf Of Lepisto, Stephen P<br>
&gt; Sent: Thursday, April 30, 2009 10:10 AM<br>
&gt; To: Discussion of IronPython<br>
&gt; Subject: Re: [IronPython] Question on Multiple Discrete IronPython<br>
&gt; sessions in a single process<br>
&gt;<br>
&gt; Dino,<br>
&gt;<br>
&gt; That example you provided produced the following output under<br>
&gt; IronPython 2.0.1:<br>
&gt;<br>
&gt; hello<br>
&gt; 42<br>
&gt;<br>
&gt; In other words, the two sessions appear to be sharing the same module<br>
&gt; in IronPython 2.0.1.<br>
&gt;<br>
&gt; Are you using a later version of IronPython where this might be fixed?<br>
&gt;<br>
&gt;<br>
&gt; -----Original Message-----<br>
&gt; From: <a href="mailto:users-bounces@lists.ironpython.com">users-bounces@lists.ironpython.com</a> [mailto:<a href="mailto:users-">users-</a><br>
&gt; <a href="mailto:bounces@lists.ironpython.com">bounces@lists.ironpython.com</a>] On Behalf Of Dino Viehland<br>
&gt; Sent: Thursday, April 30, 2009 9:28 AM<br>
&gt; To: Discussion of IronPython<br>
&gt; Subject: Re: [IronPython] Question on Multiple Discrete IronPython<br>
&gt; sessions in a single process<br>
&gt;<br>
&gt; And this works for me:<br>
&gt;<br>
&gt; using System;<br>
&gt; using IronPython.Hosting;<br>
&gt; using Microsoft.Scripting.Hosting;<br>
&gt;<br>
&gt; class foo {<br>
&gt;         static void Main(string[] args)<br>
&gt;         {<br>
&gt;             var engine = Python.CreateEngine();<br>
&gt;             ScriptScope scope1 = engine.ImportModule(&quot;foo&quot;);<br>
&gt;<br>
&gt;             var engine2 = Python.CreateEngine();<br>
&gt;             ScriptScope scope2 = engine2.ImportModule(&quot;foo&quot;);<br>
&gt;<br>
&gt;             scope1.SetVariable(&quot;foo&quot;, 42);<br>
&gt;             object res;<br>
&gt;             if(scope2.TryGetVariable(&quot;foo&quot;, out res)) {<br>
&gt;                 Console.WriteLine(res);<br>
&gt;             } else {<br>
&gt;                 Console.WriteLine(&quot;no foo&quot;);<br>
&gt;             }<br>
&gt;         }<br>
&gt;     }<br>
&gt;<br>
&gt; Foo.py:<br>
&gt; print &#39;hello&#39;<br>
&gt;<br>
&gt; Printing out:<br>
&gt;<br>
&gt; hello<br>
&gt; hello<br>
&gt; no foo<br>
&gt;<br>
&gt; &gt; -----Original Message-----<br>
&gt; &gt; From: <a href="mailto:users-bounces@lists.ironpython.com">users-bounces@lists.ironpython.com</a> [mailto:<a href="mailto:users-">users-</a><br>
&gt; &gt; <a href="mailto:bounces@lists.ironpython.com">bounces@lists.ironpython.com</a>] On Behalf Of Michael Foord<br>
&gt; &gt; Sent: Thursday, April 30, 2009 9:08 AM<br>
&gt; &gt; To: Discussion of IronPython<br>
&gt; &gt; Subject: Re: [IronPython] Question on Multiple Discrete IronPython<br>
&gt; &gt; sessions in a single process<br>
&gt; &gt;<br>
&gt; &gt; Dino Viehland wrote:<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; You mention CreateEngine but are you also creating multiple<br>
&gt; runtimes?<br>
&gt; &gt; &gt; You&#39;re only allowed 1 ScriptEngine of a given type per<br>
&gt; ScriptRuntime.<br>
&gt; &gt; &gt; So you should create a new ScriptRuntime and then get the Python<br>
&gt; &gt; &gt; engine for each runtime and then be isolated.<br>
&gt; &gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; If you call Python.CreateEngine() twice it gives you two different<br>
&gt; &gt; engine objects with what *appear* to be different runtimes.<br>
&gt; &gt;<br>
&gt; &gt; If you then call Python.ImportModule for the same module but passing<br>
&gt; in<br>
&gt; &gt; the two different engines, you get two different (non-identical<br>
&gt; &gt; objects)<br>
&gt; &gt; ScriptScopes - but changes in one are reflected in the other.<br>
&gt; &gt;<br>
&gt; &gt; Is CreateEngine not the correct way to get isolated engines?<br>
&gt; &gt;<br>
&gt; &gt; Michael<br>
&gt; &gt;<br>
&gt; &gt; &gt; *From:* <a href="mailto:users-bounces@lists.ironpython.com">users-bounces@lists.ironpython.com</a><br>
&gt; &gt; &gt; [mailto:<a href="mailto:users-bounces@lists.ironpython.com">users-bounces@lists.ironpython.com</a>] *On Behalf Of *Lepisto,<br>
&gt; &gt; &gt; Stephen P<br>
&gt; &gt; &gt; *Sent:* Thursday, April 30, 2009 8:26 AM<br>
&gt; &gt; &gt; *To:* <a href="mailto:users@lists.ironpython.com">users@lists.ironpython.com</a><br>
&gt; &gt; &gt; *Subject:* [IronPython] Question on Multiple Discrete IronPython<br>
&gt; &gt; &gt; sessions in a single process<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; Hello, everyone!<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; I am working on an service manager application that provides<br>
&gt; embedded<br>
&gt; &gt; &gt; python support through a small set of generalized classes:<br>
&gt; &gt; &gt; PythonService, PythonSession, and PythonClass. A client application<br>
&gt; &gt; &gt; asks the service manager for the PythonService object and then asks<br>
&gt; &gt; &gt; the PythonService object for a new PythonSession object. The<br>
&gt; &gt; &gt; PythonSession object is used to access embedded python through a<br>
&gt; &gt; small<br>
&gt; &gt; &gt; set of generalized methods. The PythonClass object is used to wrap<br>
&gt; a<br>
&gt; &gt; &gt; python class instance.<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; The key requirement in this is each client must have its own python<br>
&gt; &gt; &gt; session, independent of any other sessions currently running. I&#39;ve<br>
&gt; &gt; got<br>
&gt; &gt; &gt; this to work with CPython (specifically, python 2.5.4), by careful<br>
&gt; &gt; use<br>
&gt; &gt; &gt; of the global interpreter lock and swapping the thread state.<br>
&gt; &gt; &gt; IronPython 2.0.1 has a nicer way of implementing all of this by<br>
&gt; using<br>
&gt; &gt; &gt; the CreateEngine() to create a new python engine. However, in<br>
&gt; &gt; &gt; IronPython I&#39;ve run into what appears to be a significant<br>
&gt; limitation<br>
&gt; &gt; &gt; that may prevent me from using IronPython in this particular<br>
&gt; &gt; situation<br>
&gt; &gt; &gt; as an embedded language.<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; The limitation is when I import a python package from disk into<br>
&gt; &gt; &gt; IronPython (using IronPython.Hosting.Python.ImportModule()) in one<br>
&gt; &gt; &gt; session and then import the same package into a different session,<br>
&gt; it<br>
&gt; &gt; &gt; turns out that both sessions are pulling from the same module&#39;s<br>
&gt; &gt; scope.<br>
&gt; &gt; &gt; That is, if I make changes to the module&#39;s scope in one session<br>
&gt; (for<br>
&gt; &gt; &gt; example, changing a global variable), that change appears in the<br>
&gt; &gt; other<br>
&gt; &gt; &gt; session.<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; After tracing execution in the IronPython 2.0.1 code, it turns out<br>
&gt; &gt; &gt; that a module is cached in the LanguageContext (PythonContext)<br>
&gt; &gt; object,<br>
&gt; &gt; &gt; which in turn is a singleton in DLR, as it is associated with the<br>
&gt; &gt; &gt; language type. This is okay if an application is embedding<br>
&gt; IronPython<br>
&gt; &gt; &gt; itself but in my scenario, this prevents multiple discrete python<br>
&gt; &gt; &gt; sessions in a single application. Ideally, I would expect to see<br>
&gt; the<br>
&gt; &gt; &gt; system state be stored in the python engine (ScriptEngine) or<br>
&gt; python<br>
&gt; &gt; &gt; runtime (ScriptRuntime) objects.<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; Is there a way around this limitation? Can I somehow create a<br>
&gt; unique<br>
&gt; &gt; &gt; PythonContext object for each of my python sessions so I get a<br>
&gt; &gt; &gt; completely discrete python instance in each session with no<br>
&gt; &gt; crosstalk?<br>
&gt; &gt; &gt; Or do I have to resort to modifying the IronPython source to<br>
&gt; &gt; &gt; accomplish this (which I&#39;m loathe to do since then I would have to<br>
&gt; &gt; &gt; maintain it going forward)?<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; Thank you for your time and consideration in this matter.<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; -------------------------------------------------------------------<br>
&gt; --<br>
&gt; &gt; ---<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; _______________________________________________<br>
&gt; &gt; &gt; Users mailing list<br>
&gt; &gt; &gt; <a href="mailto:Users@lists.ironpython.com">Users@lists.ironpython.com</a><br>
&gt; &gt; &gt; <a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
&gt; &gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; --<br>
&gt; &gt; <a href="http://www.ironpythoninaction.com/" target="_blank">http://www.ironpythoninaction.com/</a><br>
&gt; &gt;<br>
&gt; &gt; _______________________________________________<br>
&gt; &gt; Users mailing list<br>
&gt; &gt; <a href="mailto:Users@lists.ironpython.com">Users@lists.ironpython.com</a><br>
&gt; &gt; <a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; Users mailing list<br>
&gt; <a href="mailto:Users@lists.ironpython.com">Users@lists.ironpython.com</a><br>
&gt; <a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
&gt; _______________________________________________<br>
&gt; Users mailing list<br>
&gt; <a href="mailto:Users@lists.ironpython.com">Users@lists.ironpython.com</a><br>
&gt; <a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
_______________________________________________<br>
Users mailing list<br>
<a href="mailto:Users@lists.ironpython.com">Users@lists.ironpython.com</a><br>
<a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
_______________________________________________<br>
Users mailing list<br>
<a href="mailto:Users@lists.ironpython.com">Users@lists.ironpython.com</a><br>
<a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
</blockquote></div><br>