pythoncherrypybottle

How can I get Bottle to restart on file change?


I'm really enjoying Bottle so far, but the fact that I have to CTRL+C out of the server and restart it every time I make a code change is a big hit on my productivity. I've thought about using Watchdog to keep track of files changing then restarting the server, but how can I do that when the bottle.run function is blocking.

Running the server from an external script that watches for file changes seems like a lot of work to set up. I'd think this was a universal issue for Bottle, CherryPy and etcetera developers.

Thanks for your solutions to the issue!


Solution

  • Check out from the tutorial a section entitled Auto Reloading:

    During development, you have to restart the server a lot to test your recent changes. The auto reloader can do this for you. Every time you edit a module file, the reloader restarts the server process and loads the newest version of your code.

    This gives the following example:

    from bottle import run
    run(reloader=True)
    

    Then you can distinct a child process in runtime:

    os.environ.get('BOTTLE_CHILD') == 'true'