Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Flask, Restarting with reloader: What does that mean [duplicate]

Tags:

I am trying to build y first webservice using Python Flask. I am not able to understand what does it mean for Flask to emit out Restarting with reloader, every time i run my app.

This is my code.

#!venv/bin/python
from flask import Flask
from flask import request


def buildCache():
    print 'Hello World'

buildCache()

app = Flask(__name__)


@app.route('/search')
def index():
    query = request.args.get('query','', type=str);
    return  query


if __name__ == '__main__':
    app.run(debug = True)

when i run it

venv/bin/python ./app.py
Hello World
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
Hello World

I dont understand why buildCache method is being called twice? It seems to be related to "Restarting with the reoloader', what does that mean? How do i make sure that buildCache is only executed once, before the server starts.

like image 976
konquestor Avatar asked Sep 15 '14 20:09

konquestor


People also ask

What is use reloader in Flask?

This "reloads" the code whenever you make a change so that you don't have to manually restart the app to see changes. It is quite useful when you're making frequent changes. You can turn off reloading by setting the debug parameter to False. app.run(debug=False)

Why does running the Flask dev server run itself twice?

Answer #1:The Werkzeug reloader spawns a child process so that it can restart that process each time your code changes. Werkzeug is the library that supplies Flask with the development server when you call app. run() . See the restart_with_reloader() function code; your script is run again with subprocess.

Does Flask use multiple processes?

Modern web servers like Flask, Django, and Tornado are all able to handle multiple requests simultaneously. The concept of multitasking is actually very vague due to its various interpretations. You can perform multitasking using multiprocessing, multithreading, or asyncio.


1 Answers

This "reloads" the code whenever you make a change so that you don't have to manually restart the app to see changes. It is quite useful when you're making frequent changes.

You can turn off reloading by setting the debug parameter to False.

app.run(debug=False)

"[If debug=True] the debugger will kick in when an unhandled exception occurs and the integrated server will automatically reload the application if changes in the code are detected."

Source: http://flask.pocoo.org/docs/0.10/api/#flask.Flask.debug

like image 78
Muntaser Ahmed Avatar answered Oct 10 '22 04:10

Muntaser Ahmed