<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page Section1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
        {page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=EN-US link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal>Hello, everyone!<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>I am working on an service manager application that provides
embedded python support through a small set of generalized classes:
PythonService, PythonSession, and PythonClass. A client application asks
the service manager for the PythonService object and then asks the
PythonService object for a new PythonSession object. The PythonSession
object is used to access embedded python through a small set of generalized
methods. The PythonClass object is used to wrap a python class instance.<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>The key requirement in this is each client must have its own
python session, independent of any other sessions currently running. I’ve
got this to work with CPython (specifically, python 2.5.4), by careful use of
the global interpreter lock and swapping the thread state. IronPython
2.0.1 has a nicer way of implementing all of this by using the CreateEngine()
to create a new python engine. However, in IronPython I’ve run into
what appears to be a significant limitation that may prevent me from using IronPython
in this particular situation as an embedded language.<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>The limitation is when I import a python package from disk
into IronPython (using IronPython.Hosting.Python.ImportModule()) in one session
and then import the same package into a different session, it turns out that
both sessions are pulling from the same module’s scope. That is, if
I make changes to the module’s scope in one session (for example,
changing a global variable), that change appears in the other session.<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>After tracing execution in the IronPython 2.0.1 code, it
turns out that a module is cached in the LanguageContext (PythonContext)
object, which in turn is a singleton in DLR, as it is associated with the
language type. This is okay if an application is embedding IronPython
itself but in my scenario, this prevents multiple discrete python sessions in a
single application. Ideally, I would expect to see the system state be stored
in the python engine (ScriptEngine) or python runtime (ScriptRuntime) objects.<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>Is there a way around this limitation? Can I somehow
create a unique PythonContext object for each of my python sessions so I get a
completely discrete python instance in each session with no crosstalk? Or
do I have to resort to modifying the IronPython source to accomplish this
(which I’m loathe to do since then I would have to maintain it going
forward)?<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>Thank you for your time and consideration in this matter.<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
</div>
</body>
</html>