Hot-swapping code in Python
If you watched Notch coding during Ludum Dare, you might have, like me, been jealous that he could do hot swapping of code to tweak his game engine very quickly.Well there's no reason we can't do that in Python!
Over the next week, I'm going to investigate and/or develop a framework that will perform automatic change detection and apply those changes, using the "reload" keyword of Python, to the running instance of the game.
A couple articles I'm going to look at:
http://stackoverflow.com/questions/6751263/hot-swapping-of-python-running-program
http://pypi.python.org/pypi/hotswap/0.1
If anyone has any knowledge about this already, please share!
My goal is to, by theme voting commencement (September 4), release code and/or document a strategy that anyone can use during PyWeek.
(log in to comment)
Comments
http://pastebin.com/XzTnfgAd
this pastebin has some code from a mud i coded a while back. it uses source code in the strings in get_source and set_source, does string substitution for a variables name, creates python functions and then wraps them into a property that can be added to a class...at runtime. last i knew it worked, but that was probably python2.4 and i haven't looked at the mud for years. it might be helpful, though.
The most general approach might be to save your gamestate (just to memory would suffice), drop all your datastructures, reload the modules, and load the gamestate back in a error-tolerant way. Effectively I'd focus on getting your code reloaded correctly, and risk breaking your current gamestate, which is less important in many regards.
Mat on 2011/08/27 17:46:
cool :Dinterested to see how it works...