Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change and reload python code in waitress without restarting the server?

I am using waitress to serve the web application content like.

waitress-serve --port=8000 myapp:application

While developing, as I change code, I continuously had to restart the waitress-serve to see my changes. Is there a standard way I can automate this?

like image 558
Ashok Bommisetti Avatar asked Apr 23 '16 23:04

Ashok Bommisetti


2 Answers

I know this is an old question but I had a similar issue trying to enable hot reload functionality for my REST API using Falcon framework.

Waitress does not monitor file changes but you could use hupper on top of it. Pretty simple:

$ pip install hupper
$ hupper -m waitress --port=8000 myapp:application

It works on Windows too.

like image 176
AndreFeijo Avatar answered Nov 19 '22 16:11

AndreFeijo


Based on the comment by @Dirk, I found the archive.org link to the snippet mentioned. I was able to get Waitress reloading by using Werkseug directly. Using Werkzeug's decorator run_with_reloader causes the app to restart whenever a Python file changes. (Werkzeug is used within Flask so it should be available).

Additionally, the app.debug = True line causes Flask to reload template files when they change. So you may want both considering your particular situation.

import werkzeug.serving

@werkzeug.serving.run_with_reloader
def run_server():
    app.debug = True
    waitress.serve(app, listen='127.0.0.1:4432')

if __name__ == '__main__':
    run_server()

Once I had my server set up this way, it auto-reloaded the server whenever any file changed.

like image 23
Ethan T Avatar answered Nov 19 '22 16:11

Ethan T