0

The problem I want to solve is to hide this message from printing when starting the flask server:

 * Serving Flask app "for_so" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on

The solution I found is this (reference: https://stackoverflow.com/a/56103584/14531062):

import os
os.environ['WERKZEUG_RUN_MAIN'] = 'true'

This does hide it but there is a slight issue that I have a shutdown route and I am also running the flask app in another thread and what this does is when shutting down the server (reference: https://stackoverflow.com/a/17053522/14531062 (I wasn't able to run this with Process)) it shuts down the whole process with Exit code 15, without it, the process still keeps running but the application has been stopped:

from flask import Flask, request
from threading import Thread

# without this part it shuts down the server fine and keeps running
# with this part it apparently shuts down the whole process
import os
os.environ['WERKZEUG_RUN_MAIN'] = 'true'


app = Flask(__name__)


@app.route('/shutdown')
def shutdown():
    func = request.environ.get('werkzeug.server.shutdown')
    func()
    return 'Shutting down...'


def run_app():
    Thread(target=app.run, kwargs={'debug': True, 'use_reloader': False}).start()


if __name__ == '__main__':
    run_app()
    while True:
        pass

Requirements are that I need to keep the rest of the logging messages and I need to be able to shut down the server without shutting down the rest of the process. So either there is another way to hide that message or another way to shut down the server.

Matiiss
  • 5,203
  • 2
  • 11
  • 27
  • I found the solution here: https://stackoverflow.com/a/45017691/14531062 (previously didn't try), it allows to shut down the server without causing the whole process to exit (at least so it seems so far) and I can use that `Werkzerug_main`ish thingy – Matiiss Aug 06 '21 at 17:16

0 Answers0