Created
December 6, 2017 04:19
-
-
Save dyerrington/1b1514e4389a3369ebdcb81ab81bd5dc to your computer and use it in GitHub Desktop.
This basic Python snippet is a basic pattern to automatically reload the contents of a file after it has changed. You can also adapt this example to extend the "watching" to include all files and directories within current context if you want to also trigger a reload on file modification / change.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, sys | |
from time import sleep | |
## We could update this to all files in all subdirectories | |
watching = [__file__] | |
watched_mtimes = [(f, os.path.getmtime(f)) for f in watching] | |
while True: | |
print("Idle...") | |
sleep(2) ## So not to kill our CPU cycles | |
for f, mtime in watched_mtimes: | |
if os.path.getmtime(f) != mtime: | |
print("SOmehtign changed sucka reloading and exploding!!") | |
os.execv(sys.executable, ['python'] + sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment