Proper format of Procfile for Flask App on Heroku - python

I'm trying to deploy a flask app on heroku. I've gotten to the point where the app builds and deploys, but when I try to go to the URL, the app times out with the following error.
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
I think the problem is with my procfile. It has one line.
web: python add_entry3.py
Other people have procfiles that look like this:
web: gunicorn app:app
This is just a toy app and I don't care about performance so I don't think I need to use gunicorn for the web server. Should I be putting a colon and command after my app's file name (add_entry3.py)?

Most likely your flask app isn't answering on the port and interface the Heroku expects. By default, Flask only listens on 127.0.0.1, and I think on port 5000. Heroku passes your app a PORT environment variable and you'd need to tell Flask to listen on all interfaces.
But there are reasons other than performance you want to avoid Flask's default debug server for production code. It's got memory leaks, there are security implications, and really ... just don't do it. Add gunicorn to your requirements.txt and use that.
But if you must use the Flask test/debug server, change your app.run() call to something like this:
app.run(host='0.0.0.0', port=int(os.environ.get("PORT", 5000)))

Related

How do I use a specific host in an h2o-wave app

Currently trying to run an h2o-wave app on a remote server. I'm restricted to using 0.0.0.0 as the host on the server (the specific port is not as restrictive).
I've looked at the h2o configuration documentation and tried several variations of what they suggest:
H2O_WAVE_INTERNAL_ADDRESS=ws://0.0.0.0:8000
H2O_WAVE_EXTERNAL_ADDRESS=ws://0.0.0.0:8000
H2O_WAVE_APP_ADDRESS=ws://0.0.0.0:8000
But, the app is still running on the default localhost: http://127.0.0.1:8000
Just setting H2O_WAVE_APP_ADDRESS is enough.
$ H2O_WAVE_APP_ADDRESS=http://0.0.0.0:8000 uvicorn my_app:main

Unable to deploy python app on heroku, port binding error

I'm trying to deploy the following python app on an heroku server:
https://github.com/PX4/flight_review
The main server script is called ./serve.py, you will find it under:
https://github.com/PX4/flight_review/blob/master/serve.py
It's a tornado web server, but I m not sure on how to configure it, on my local machine I respond to the url http://localhost:5006/
In added a Procfile as follow:
web: ./serve.py --host 0.0.0.0:5006
and also tried with and without other arguments for the port, and also added a PORT variable into the env variable, but I constantly get the following error:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
Any idea on what could go wrong?
Thanks in advance!
Following the documentation about Procfiles:
You have to just use the $PORT environment variable, which is set by Heroku when trying to start your dyno.
In your case that would seem to be:
web: ./serve.py --host 0.0.0.0:$PORT

How can I increase gunicorn --limit-request-line in a python App Engine application with Flask?

It is a sort question, I am building a very simple API with python runtime and Flask in Google Cloud Platform. Problem is that the request URL can get a bit long (approx 6000 bytes) and the current gunicorn limit is 4096. I know it will sound dumb, but I am trying to change it on the app.yaml file and when I run it it returns 500 server error. I am almost certain it is a problem in the entrypoint that I added because I do not know how to modify the app.yaml (I am new in web development). My app.yaml is as follows:
runtime: python37
service: py
entrypoint: gunicorn --limit-request-line 8190 main:app
Could someone help me if you know what error I have? I have checked GCP documentation and demos but as usual they are not very descriptive.
You may need to have gunicorn in your requirements.txt if you don't have it already, as the docs mention.
Regarding the entrypoint in your app.yaml it looks correct, however I think you're missing adding the port to have App Engine listening to 8080, which is a requirement.
entrypoint: gunicorn -b :8080 --limit-request-line 8190 main:app
More information can be found as well in the following section of the documentation.

Heroku Flask - Deploy a 'modular' app from tutorial not working, foreman start works locally

based on this structure: http://flask.pocoo.org/docs/patterns/packages/
I also tried this post: Deploying Flask app to Heroku
I am having trouble getting this to work on heroku. I usually get the PORT does not set within 60 seconds error. I have read other SO posts and just can't figure out if my project structure is wrong or my procfile. I tried other ports than 5000 as well.
Here is my current project structure:
/myapplication
Procfile
runserver.py
/applicationfolder
__init__.py
views.py
Here is my Procfile
web: python runserver.py $PORT
Here is my runserver.py
from applicationfolder import app
app.run()
if __name__ == '__main__':
import os
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
Here is my init.py
import os
from flask import Flask
from flask import render_template, jsonify, request
app = Flask(__name__)
app.config.from_object('config')
import applicationfolder.views
From there views.py runs.
This works locally with foreman start and python runserver.py, but does not work with heroku. I have tried many things with PORT but port doesn't seem to set even with a different PORT than 5000. I think it has something to do with my project structure.
The app.run() was in there twice, which as you noted is what's screwing things up. The app.run() invokes a simply pure-python development server so that you can easily run and/or debug your script.
By invoking it at the module level (right under your import in runserver.py), you were effectively trying to start the development server as the python code was loaded, and then when it went to run it when invoked from the Procfile, the development server was already in flight, having been starting with it's defaults (latest version of Flask is pulling relevant defaults from the SERVER_NAME environment variable). By having it in both places, you were trying to invoke that method twice.
You basically want either the straight up module load (in which case, kill off the code under "if name ...", or you use the code when invoking under main, in which case don't start the service at module load time.

How to run python websocket on gunicorn

I found this 0 dependency python websocket server from SO: https://gist.github.com/jkp/3136208
I am using gunicorn for my flask app and I wanted to run this websocket server using gunicorn also. In the last few lines of the code it runs the server with:
if __name__ == "__main__":
server = SocketServer.TCPServer(
("localhost", 9999), WebSocketsHandler)
server.serve_forever()
I cannot figure out how to get this websocketserver.py running in gunicorn. This is because one would think you would want gunicorn to run server_forever() as well as the SocketServer.TCPServer(....
Is this possible?
GUnicorn expects a WSGI application (PEP 333) not just a function. Your app has to accept an environ variable and a start_response callback and return an iterator of data (roughly speaking). All the machinery encapsuled by SocketServer.StreamRequestHandler is on gunicorn side. I imagine this is a lot of work to modify this gist to become a WSGI application (But that'll be fun!).
OR, maybe this library will get the job done for you: https://github.com/CMGS/gunicorn-websocket
If you use Flask-Sockets extension, you have a websocket implementation for gunicorn directly in the extension which make it possible to start with the following command line :
gunicorn -k flask_sockets.worker app:app
Though I don't know if that's what you want to do.

Categories