see the reproduction code below.
Tracing a memory leak I found that reload(module) does not immediately take effect.
The program below should print 0,1,2,3,4 but, when executed fast it prints sequences like 0,0,0,3,3 and such. Increasing the time in the sleep() function to eg 1 second seems to fix this.
Note that this code is a boiled down version of more practical code just to reproduce the problem, I need to deal with a situation in a real life app.
Does anybody have an idea how to ensure stability?
I'm on windows, cpython27 32 bit.
Thanks for reading this.
# this program assumes folder lib\mymodule exists and contains __init__.py import time import io import gc modulefile = 'c:\\python27\\lib\\mymodule\\simplemodule.py' for cnt in range(5): modulecode = """def runmodule(): return %i """%(cnt) obj = io.open(modulefile, u'wb') obj.write(modulecode) obj.close() if cnt==0: import mymodule.simplemodule else: reload(mymodule.simplemodule) gc.collect() print mymodule.simplemodule.runmodule() time.sleep(0.05)