reloading updated modules in long running server?

Pierre Quentel quentel.pierre at wanadoo.fr
Sat Oct 16 05:17:31 EDT 2004


I don't know if there is a way to catch the event "a file has been 
modified in the file system" but you can always check if something has 
changed in sys.modules every time a request is processed by the server, 
or every n second

Something like this should work :

updateTime={}
for m in sys.modules.values():
     if m and hasattr(m,"__file__"):
         modTime=os.path.getmtime(m.__file__)
         if updateTime.has_key(m):
             if modTime != updateTime[m]:
                 reload(m)
                 updateTime[m]=modTime
         else:
             updateTime[m]=modTime

Pierre

aurora a écrit :
> I am looking for a way for reloading updated modules in a long running  
> server. I'm not too concerned about cascaded reload or objects already  
> created. Just need to reload module xxx if the corresponding xxx.py is  
> updated.
> 
> I found something useful in module.__file__. Would it work if I use it 
> to  generate filenames xxx.py xxx.pyc and then compare their mtime? I'm 
> not  too sure about the mechanism of generation of .pyc file. Would it 
> be too  system specific to reply on?



More information about the Python-list mailing list