Tidbits @ Kassemi

A collection of opinions, thoughts, tricks and misc. information.

Friday, January 13, 2006

 

CherryPy Infinite Autoreload after Kid integration...

HA! This sucked... CherryPy v.2.1.0 and Kid 0.8. I've spent the last four hours trying to figure out why I couldn't get Kid to work properly with CherryPy... Even the simplest little examples from the website wouldn't function. Whenever I would start the Cherrypy server, it would continue to run, automatically starting itself again and again... The simple solution was to turn off autoreload by setting the server up as production:

cherrypy.config.update({'server.environment':'production'})

But not having the automatic reload on during development is a pain. I don't want to have to continue to restart my server whenever I make the tiniest change in the code... I like to see the instant results... The way I remember with PHP... Alt-F1 (Move to browser workspace), CTRL-R (reload page). So I needed to find a fix, and I started working through all of tracks to see if anything related. I came across a change to the autoreload abilities of cherrypy made by turbogears... It told me where autoreload was, and what it was called, so I went and hacked some source...

I ended up changing the cherrypy/lib/autoreload.py file in cherrypy. Replace the reloader_thread() function with this (don't say a thing about indentation. Blogger messes that up even with pre tags... So, if you found this helpful, get me some god damned hosting!):


def reloader_thread():
mtimes = {}

def fileattr(m):
return getattr(m, "__file__", None)

while RUN_RELOADER:
modlist = sys.modules.values()
for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), modlist)):
if filename and os.path.exists(filename):
if filename.endswith(".kid") or filename == "":
continue
if filename.endswith(".pyc"):
filename = filename[:-1]
try:
mtime = os.stat(filename).st_mtime
except OSError, e:
sys.exit(3) # force reload
if filename not in mtimes:
mtimes[filename] = mtime
continue
if mtime > mtimes[filename]:
sys.exit(3) # force reload
time.sleep(1)


And restart your server... Hopefully, you'll have the same luck I did, and be able to use your server in development mode again!

The problem was simply that cherrypy didn't like eggs that were stored in zip format, and OSError would be raised whenever it encountered one. The biggest change is the os.path.exists() call, which makes sure that cherrypy thinks that an egg is a directory, and it isn't, it won't call it... It seems to work for now, but again, I don't know what the side affects are, so use at your own risk...

Good day,
James

Comments:
Thank you. Thank you. Thank you!

Can't believe this went out in a full release!
 
Post a Comment



<< Home

Archives

August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006  

This page is powered by Blogger. Isn't yours?