I’m currently playing with Flask and I can’t figure out how the debug mechanism is working. To be more precise when I save the python file with my application I don’t need to restart the server, it will be loaded automatically when I make a request. So my question is how does the running program know it was changed and respond to that changes?
Share
Flask is using Werkzug’s underlying
run_with_reloaderfunction (found inserving.py) … which is itself using therestart_with_reloaderandreloader_loopfunction created earlier in the same file.run_with_reloaderspawns another python process (running Werkzug again with all the same arguments that you passed to the first one) and this new processes uses thethreadmodule to spawn a new thread or subprocess that runs your server function. It then runs thereloader_loopand waits.reloader_loopsimply loops through all the modules that have been imported and gets their last modified dates. Then at specified intervals (which defaults to 1 s) it checks all the files again to see if they’ve been modified. If they have, the currently running (slave) Werkzug process exits (terminates) with an exit code of 3. Once it exits, the thread or subprocess it started (which is actually doing the work) is terminated as well. The master process checks to see if the exit code was 3. If it was, it spawns a new slave subprocess, just as it did before. Otherwise, it exits with the same exit code.Here’s the code for reference: