Skip to content

Instantly share code, notes, and snippets.

@dyerrington
Created December 6, 2017 04:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyerrington/1b1514e4389a3369ebdcb81ab81bd5dc to your computer and use it in GitHub Desktop.
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.
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