[IronPython] Statefulness of CompiledCode

Greg Parker greg.parker at brovada.com
Mon May 26 17:54:04 CEST 2008

I am currently involved in a project that is using IronPython.  I am 
surprised by an issue that has popped up recently during testing.  To 
efficiently process data as it arrives we have compiled the python 
script into a CompiledCode object once at startup.  Every time we need 
to process incoming data we simply call execute on the CompiledCode 
instance after setting up some global attributes.  The problem is that 
the CompiledCode instance seems to be stateful.  Certain class 
variables, et cetera, are retaining their values from one call to 
execute to the next.  Is this desired behavior?  If so, how should I be 
handling this?  I did attempt to create a brand new PythonEngine 
instance to service each transaction, but this seemed to leak memory 
very badly, and eventually crashed the entire application.

Here is a condensed version of the python script to illustrate the 
problem.  I run this from a C# application which executes the 
CompiledCode ten times.  When the length of "fooListClassVariable" is 
printed, it increases to ten.  I would have expected to see one for 
every iteration.

class SomeClass:
    fooListClassVariable = [];
    def AddFoo(self):
        print str(len(self.fooListClassVariable))

from SomeInclude import SomeClass


---- C# Application---------------------
pyEngine = new PythonEngine();
String py = LoadMyScript("Program.py"));
compiledScript = pyEngine.Compile(py);

for (int i = 0; i < 10; i++) {

More information about the Ironpython-users mailing list