Hot-swapping Of Python Running Program
Solution 1:
You could poll the runtime.py file, waiting for it to change. Once it changes, just call
reload(runtime)
Any time I'm debugging a python module, I use this approach in the interactive python command prompt (except I manually call reload(), I don't poll anything).
EDIT: To detect changes in a file, check out this SO question. Polling may be the most reliable option, but I would only reload the file if the modified time is updated, rather than reloading it on every poll. You should also consider catching exceptions when you reload, especially syntax errors. And you may or may not encounter problems with thread safety.
Solution 2:
globe = __import__('copy').copy(globals())
whileTrue:
withopen('runtime.py', 'r') as mod:
exec mod in globe
__import__('time').sleep(1)
Will repeatedly read and run runtime.py
with a nearly unpolluted globals()
and no locals()
, and won't pollute the global scope, but all of runtime's namespace will be available in globe
Post a Comment for "Hot-swapping Of Python Running Program"