Python reload module does not take effect immediat

2019-07-03 10:52发布

问题:

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)

回答1:

The problem is that a pyc won't regenerate unless it is out-of-date with respect to the undelying py file. If the file modification times are checked with a one second resolution, your updates are likely being ignored and the out-of-date pyc is invoked rather than your updated source.

  • Try removing the pyc file between each import or reload.

  • Alternatively, you can the set the environment variable PYTHONDONTWRITEBYTECODE=1.

  • You can also bypass the import logic by using execfile.