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.
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)
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With