Perform work before first request with bottle/Uwsgi/nginx - python

I have a bottle web server running with UWSGI and Nginx. Normally, if I start the script manually with
python app.py
I'm able to do
if __name__ == '__main__':
spawn_workers()
init_workspace()
run(app, host='0.0.0.0', port=8080)
the issue is, when using UWSGI, is that it doesn't run the if __name__ == '__main__' part, because it just grabs the app = application = bottle.Bottle() part. This means I never have a chance to spawn background workers before the webserver is started.
What's the proper way around this? I see that flask has a app.before_first_request function, but I couldn't find anything that corresponds to that feature for bottle.

Is it not sufficient to just perform your initialization in app.py? E.g.,
# app.py
spawn_workers()
init_workspace()
app = Bottle()

Related

How to run server in development mode with flask?

I am learning to use flask and I want to run the server for an application in development mode, for this I do the following:
app = Flask(__name__)
if __name__=="__main__":
os.environ["FLASK_ENV"] = "development"
app.run(debug=True)
When I run I get the following in the terminal:
enter image description here
Environment:development does not appear to me as I understand it should appear. In fact, before doing this I don't get Environment:production either, I don't know what's going on. As a consequence, every time I want to see the changes that I am making in the code, I have to stop the server and run it again since the changes are not seen when refreshing the page.
If you're goal is for the application to restart each time code changes are saved, it shouldn't require any more than the following:
app = Flask(__name__)
if __name__=="__main__":
app.run(debug=True)
If you want to see what all your app config variables are set to by default, you can add the following line above app.run
print(app.config)
If you wanted to change your environment to production, change the 'ENV' variable after you initialize app
app = Flask(__name__)
app.config['ENV'] = 'production'
if __name__=="__main__":
app.run(debug=True)

Serving dynamic files on heroku?

I have a flask app that has two threads. One modifies flask templates to keep them up to date from scraped info, and the other is the flask server that takes incoming route requests.
from flask import Flask, render_template
import threading
import RunArbIfDown
app = Flask(__name__, static_url_path='')
#app.route('/')
def index():
return render_template('index.html')
if __name__ == "__main__":
threading.Thread(target=app.run).start()
threading.Thread(target=RunArbIfDown.start).start() # this line continuously updates index.html (every 60s)
When I check the app, index.html is never updated even though there seem to be no errors. Are we allowed to modify files on the heroku dyno? Are there any good solutions for this?
Running threads in a WSGI environment might yield unexpected results. The WSGI server usually manages threads and can create and kill them at any time.
Also you have protected your threading code with a __name__ condition. The code will execute if the file is started directly. A WSGI server will not do that. It will import the file and the the condition will not be met.
A typical way to run recurring tasks in Flask is to use a custom command via cron. It is run as a separate process.

Python script with flask server takes too long to exit on Windows

I'm new to Python.
As a part of a project I'm trying to deploy a Flask server locally, through the Windows command line.
My Python version is 3.6.0.
The code:
from flask import Flask
app = Flask(__name__)
#app.route('/') def index():
return '<h1>Hello World!</h1>'
if __name__ == "__main__":
app.run()
The problem:
It's about killing the script as it runs. Launching this script with python deploy.py and hitting CTRL+C shuts it off.
BUT - if I hit access that '/' route via the browser once or more, and a moment later try to kill the script in the same manner, then it would take about 10 seconds of nothing until it responds and is finally killed.
Why is this happening? How can I shut the server off immediately each time for continuous and quick development?
Thanks!!
Well if your goal is continuous and quick development, then you can change flask's configuration.
Best solution for your problem would be setting the DEBUG setting to True. If DEBUG is set to True, then flask will automatically reload the server on code changes.
There are a few ways to do this but the easiest one(because you said you are a beginner) is to pass the debug argument to app.run()
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return '<h1>Hello World!</h1>'
if __name__ == "__main__":
app.run(debug=True)

flask 'hello world' not working

I copy pasted the flask's 'hello world' app from their website and am trying to run it. I get an error message in Chrome saying
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
Here is the 'hello world' app straight from flasks website
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.debug = True
app.run()
What I have tried:
-temporarily disabling Avast!
-disabling windows firewall
-ensuring that the flask module is installed
This was working a couple days ago actually...
I don't know why but when I change
app.run()
to
app.run(port=4996)
it starts working. No idea why the default port is throwing an error. Oh well.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'Hello World'
if __name__ == '__name__':
app.run()
app.run(port=5000)
For Windows machines you can use the command in cmd:
set FLASK_APP=python_file.py
flask run
Some other process is running on port 5000. It may be you still have an old Flask process running, with broken code. Or a different web server altogether is running on that port. Shut down that process, or run on a different port.
You can switch to using a different port with the port argument to app.run():
app.run(port=8080)
If you can't figure out what process is still bound to port 5000, use the Windows Resource Monitor or run netstat -a -b from a command line. See How can you find out which process is listening on a port on Windows?
I think you are trying to copy the route generated through your flask program in cmd by pressing ctrl+c which quits your running flask program . i was also doing the same.just try to type the route generated by your flask program on your browser . it will definitely resolve your problem.
Where your python file store is, use cmd and then go on your file store directory, then
set FLASK_APP=filename.py
After this your flask run cmd will work.
from flask import Flask
app = Flask(__name__) # creating app
#app.route('/', methods['GET']) #routing it to the home page
def home(): #function
return "hello world"
app.run(port=5000, debug=true) #function call by the app
Add port and use methods whatever your need is USE GET in your case and try to remove your cache and run the this code it will definitely work.

Threaded Flask application not working as expected

I want my flask application to be able to process more than one call at the same time.
I've been testing running with threaded=True or processes=3 with the code below but when I make two calls to the server the later always have to wait for the first one to complete.
I know that it's recommended to deploy the application on a more sophisticated WSGI container but for now I just want my small app to be able to process 2 calls at once.
from flask import Flask, Response, stream_with_context
from time import sleep
app = Flask(__name__)
def text_gen(message):
for c in message:
yield c
sleep(1)
#app.route("/")
def hello():
stream = text_gen('Hello World')
return Response(stream_with_context(stream))
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080, debug=True, threaded=True)
#Lukas was right.
I was debugging in Google Chrome with two tabs. Apparently Chrome is trying to be smart by using the socket same for both tabs. How Chrome handles that can be changed with the -socket-reuse-policy flag when starting Chrome.
An easier way to test is by using different hostname in each tab or by using curl -N (-N flag for no buffer to see the streaming). Doing that it did indeed work as expected.

Categories