Home > Blockchain >  Waitress serve takes too long to run flask server
Waitress serve takes too long to run flask server

Time:07-23

I am trying to run a flask app with waitress serve, but when i call "python app.py" nothing happens (i waited 2 minutes then ctrl c). When i run flask with app.run() everything works normally. Some help would be appreciated

import Flask
from waitress import serve

app=Flask(__name__)
<My code>
if __name__ == "__main__":
  serve(app,port="8080",host="0.0.0.0")

CodePudding user response:

Create a method called create_app() for example which returns the Flask object. You can add a route if you wish.

from flask import Flask

def create_app():
    app = Flask(__name__)

    @app.route('/')
    def hello_world():
      return 'Hello World!'
    return app

if your file is called app.py run the following command.

waitress-serve --port 8080 --call "app:create_app"

Output:

INFO:waitress:Serving on http://0.0.0.0:8080

Edit


If you really want to do it with waitress.serve() you can do this. For some reason this only works on localhost (127.0.0.1)

With your code the server starts and is accessable (the only thing you need are some routes) the only thing that is missing is terminal output.

You can activate logging like this:

import logging
logger = logging.getLogger('waitress')
logger.setLevel(logging.INFO)

This causes the output:

INFO:waitress:Serving on http://127.0.0.1:8080

If you want to see every request: install paste pip install paste and use TransLogger

from paste.translogger import TransLogger
# modify serve function
serve(TransLogger(app, setup_console_handler=False), host='127.0.0.1', port=8080)

This causes the following output on every request.

INFO:wsgi:127.0.0.1 - - [21/Jul/2022:15:40:25  0200] "GET / HTTP/1.1" 200 12 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"

Of course this depends on your browser.

Full code:

from flask import Flask
from waitress import serve
from paste.translogger import TransLogger
import logging
logger = logging.getLogger('waitress')
logger.setLevel(logging.INFO)

app = Flask(__name__)

@app.route('/')
def hello():
  return 'Hello World!'


serve(TransLogger(app, setup_console_handler=False), host='127.0.0.1', port=8080)
  • Related