reloading updated modules in long running server?

A

aurora

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?
 
P

Pierre Quentel

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 :
 
B

Bryan

Pierre said:
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

if you have class A in module a.py and class B(A) in module b.py, and in module c.py you instantiale b = B(). will
there be a problem if you reload module b, then c, then a? in the previous example, m in sys.modules.values() doesn't
take this into account.

bryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top