Python Flask on Heroku repeatedly logs "Permission Denied" - python

My Python Flask app is able to git push to heroku no problem, but after pushing, when checking the logs with heroku logs --tail, it repeatedly logs:
2022-08-24T17:07:47.065727+00:00 app[web.1]: [2022-08-24 17:07:47 +0000] [21] [INFO] Booting worker with pid: 21
2022-08-24T17:07:47.100592+00:00 app[web.1]: * Serving Flask app 'app'
2022-08-24T17:07:47.100599+00:00 app[web.1]: * Debug mode: off
2022-08-24T17:07:47.101990+00:00 app[web.1]: Permission denied <-- here
2022-08-24T17:07:47.102149+00:00 app[web.1]: [2022-08-24 17:07:47 +0000] [21] [INFO] Worker exiting (pid: 21)
Over and over again with different pid.
My project structure:
MyApp\
src\
static\...
templates\...
app.py
Procfile
requirements.txt
My Procfile is web: gunicorn --pythonpath src app and my app.py is:
# ... imports
app = Flask(__name__)
# ... routes
app.run(host="0.0.0.0", port=80, threaded=True)
I don't know what permission is being denied, or how it is being denied, or how to fix it. Tried searching around, all the results were for "[Errno ...] Permission Denied", which is different from mine.

heroku has set the port itself, so you can't assign flask port=80 here
try this
app.run(host="0.0.0.0", port=int(os.getenv('PORT', 80)), threaded=True)
remember to "import os" at the top
hope this help

Related

Dash/Flask Python App Azure Deployment: Application object must be callable

so I'm trying to deploy my Dash app onto Azure using Azure Web App. When I use the "Github Actions" method of deployment, I get the following (I even made my repository public for this):
Package deployment using ZIP Deploy initiated.
##[error]Failed to deploy web package to App Service.
##[error]Deployment Failed with Error: Error: Failed to deploy web package to App Service.
Unauthorized (CODE: 401)
##[warning]Error: Failed to update deployment history.
Unauthorized (CODE: 401)
App Service Application URL: http://my-site.azurewebsites.net
I've also tried deploying with "Kudu App Build" instead of the Github Actions and that "deploys successfully", giving me a status of "Success(Active)" but once I go to my website it fails, then when I go to Diagnosis and solve problems in the azure portal I receive the following error:
2020-06-05T14:32:08.567155259Z
2020-06-05T14:32:08.567159059Z Documentation: http://aka.ms/webapp-linux
2020-06-05T14:32:08.567163159Z Python 3.8.0
2020-06-05T14:32:08.567166959Z Note: Any data outside '/home' is not persisted
2020-06-05T14:32:09.126144120Z Starting OpenBSD Secure Shell server: sshd.
2020-06-05T14:32:09.177989757Z Site's appCommandLine: gunicorn --bind=0.0.0.0 --timeout 600 application:app
2020-06-05T14:32:09.179066899Z Launching oryx with: -appPath /home/site/wwwroot -output /opt/startup/startup.sh -virtualEnvName antenv -defaultApp /opt/defaultsite -userStartupCommand 'gunicorn --bind=0.0.0.0 --timeout 600 application:app'
2020-06-05T14:32:09.289253728Z Oryx Version: 0.2.20200114.13, Commit: 204922f30f8e8d41f5241b8c218425ef89106d1d, ReleaseTagName: 20200114.13
2020-06-05T14:32:09.315423056Z Found build manifest file at '/home/site/wwwroot/oryx-manifest.toml'. Deserializing it...
2020-06-05T14:32:09.322220623Z Build Operation ID: |mqhAw8K+i4s=.a5925a0c_
2020-06-05T14:32:09.937709104Z Writing output script to '/opt/startup/startup.sh'
2020-06-05T14:32:10.656204831Z Found virtual environment .tar.gz archive.
2020-06-05T14:32:10.656991662Z Removing existing virtual environment directory /antenv...
2020-06-05T14:32:10.667494675Z Extracting to directory /antenv...
2020-06-05T14:32:42.888599159Z Using packages from virtual environment antenv located at /antenv.
2020-06-05T14:32:42.891102058Z Updated PYTHONPATH to ':/antenv/lib/python3.8/site-packages'
2020-06-05T14:32:44.966320399Z [2020-06-05 14:32:44 +0000] [42] [INFO] Starting gunicorn 20.0.4
2020-06-05T14:32:44.973255871Z [2020-06-05 14:32:44 +0000] [42] [INFO] Listening at: http://0.0.0.0:8000 (42)
2020-06-05T14:32:44.974184808Z [2020-06-05 14:32:44 +0000] [42] [INFO] Using worker: sync
2020-06-05T14:32:45.047728498Z [2020-06-05 14:32:45 +0000] [44] [INFO] Booting worker with pid: 44
2020-06-05T14:32:50.315649084Z Application object must be callable.
2020-06-05T14:32:50.321677821Z [2020-06-05 14:32:50 +0000] [44] [INFO] Worker exiting (pid: 44)
2020-06-05T14:32:50.630621560Z [2020-06-05 14:32:50 +0000] [42] [INFO] Shutting down: Master
2020-06-05T14:32:50.630656561Z [2020-06-05 14:32:50 +0000] [42] [INFO] Reason: App failed to load.
That was the application error, here is the container error
Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
2020-06-05T14:30:22.831Z INFO - Initiating warmup request to container my-site_0_06a70784 for site my-site
2020-06-05T14:30:38.890Z INFO - Waiting for response to warmup request for container my-site_0_06a70784. Elapsed time = 16.0594968 sec
2020-06-05T14:30:54.492Z INFO - Waiting for response to warmup request for container my-site_0_06a70784. Elapsed time = 31.6616936 sec
2020-06-05T14:31:09.663Z ERROR - Container my-site_0_06a70784 for site my-site has exited, failing site start
2020-06-05T14:31:09.695Z ERROR - Container my-site_0_06a70784 didn't respond to HTTP pings on port: 8000, failing site start. See container logs for debugging.
2020-06-05T14:31:10.217Z INFO - Stopping site my-site because it failed during startup.
2020-06-05T14:31:11.646Z INFO - Starting container for site
2020-06-05T14:31:11.647Z INFO - docker run -d -p 4040:8000 --name my-site_0_9c00d1f9 -e WEBSITE_SITE_NAME=my-site -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=my-site.azurewebsites.net -e WEBSITE_INSTANCE_ID=b78e390e9ef390f579e4e316c4d4ea6c7f187e4af48b856ebd3562fda3c5ef4f appsvc/python:3.8_20200101.1 gunicorn --bind=0.0.0.0 --timeout 600 application:app
Here is what my application.py looks like (I've tried several variations of this to no avail):
import app as application
app = application.app
if __name__ == "__main__":
app.run_server(debug=False)
And at the top of my app.py file I have:
app = dash.Dash(__name__)
This is my requirements.txt:
bs4==0.0.1
dash==1.12.0
dash-table==4.7.0
Flask==1.1.2
numpy==1.18.0
pandas==1.0.4
requests==2.12.4
Lastly, this is the startup command I use in the Configuration->General Settings->Startup Command
gunicorn --bind=0.0.0.0 --timeout 600 application:app
The application you should run when deployment is dash.Dash.server(in your case it's app.server), it's the underly Flask application. So, you have to update the second line of your application.py to this:
app = application.server
See more in Dash docs.

"[CRITICAL] WORKER TIMEOUT" in logs when running "Hello Cloud Run with Python" from GCP Setup Docs

Following the tutorial here I have the following 2 files:
app.py
from flask import Flask, request
app = Flask(__name__)
#app.route('/', methods=['GET'])
def hello():
"""Return a friendly HTTP greeting."""
who = request.args.get('who', 'World')
return f'Hello {who}!\n'
if __name__ == '__main__':
# Used when running locally only. When deploying to Cloud Run,
# a webserver process such as Gunicorn will serve the app.
app.run(host='localhost', port=8080, debug=True)
Dockerfile
# Use an official lightweight Python image.
# https://hub.docker.com/_/python
FROM python:3.7-slim
# Install production dependencies.
RUN pip install Flask gunicorn
# Copy local code to the container image.
WORKDIR /app
COPY . .
# Service must listen to $PORT environment variable.
# This default value facilitates local development.
ENV PORT 8080
# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn --bind 0.0.0.0:$PORT --workers 1 --threads 8 app:app
I then build and run them using Cloud Build and Cloud Run:
PROJECT_ID=$(gcloud config get-value project)
DOCKER_IMG="gcr.io/$PROJECT_ID/helloworld-python"
gcloud builds submit --tag $DOCKER_IMG
gcloud run deploy --image $DOCKER_IMG --platform managed
The code appears to run fine, and I am able to access the app on the given URL. However the logs seem to indicate a critical error, and the workers keep restarting. Here is the log file from Cloud Run after starting up the app and making a few requests in my web browser:
2020-03-05T03:37:39.392Z Cloud Run CreateService helloworld-python ...
2020-03-05T03:38:03.285477Z[2020-03-05 03:38:03 +0000] [1] [INFO] Starting gunicorn 20.0.4
2020-03-05T03:38:03.287294Z[2020-03-05 03:38:03 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080 (1)
2020-03-05T03:38:03.287362Z[2020-03-05 03:38:03 +0000] [1] [INFO] Using worker: threads
2020-03-05T03:38:03.318392Z[2020-03-05 03:38:03 +0000] [4] [INFO] Booting worker with pid: 4
2020-03-05T03:38:15.057898Z[2020-03-05 03:38:15 +0000] [1] [INFO] Starting gunicorn 20.0.4
2020-03-05T03:38:15.059571Z[2020-03-05 03:38:15 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080 (1)
2020-03-05T03:38:15.059609Z[2020-03-05 03:38:15 +0000] [1] [INFO] Using worker: threads
2020-03-05T03:38:15.099443Z[2020-03-05 03:38:15 +0000] [4] [INFO] Booting worker with pid: 4
2020-03-05T03:38:16.320286ZGET200 297 B 2.9 s Safari 13 https://helloworld-python-xhd7w5igiq-ue.a.run.app/
2020-03-05T03:38:16.489044ZGET404 508 B 6 ms Safari 13 https://helloworld-python-xhd7w5igiq-ue.a.run.app/favicon.ico
2020-03-05T03:38:21.575528ZGET200 288 B 6 ms Safari 13 https://helloworld-python-xhd7w5igiq-ue.a.run.app/
2020-03-05T03:38:27.000761ZGET200 285 B 5 ms Safari 13 https://helloworld-python-xhd7w5igiq-ue.a.run.app/?who=me
2020-03-05T03:38:27.347258ZGET404 508 B 13 ms Safari 13 https://helloworld-python-xhd7w5igiq-ue.a.run.app/favicon.ico
2020-03-05T03:38:34.802266Z[2020-03-05 03:38:34 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:4)
2020-03-05T03:38:35.302340Z[2020-03-05 03:38:35 +0000] [4] [INFO] Worker exiting (pid: 4)
2020-03-05T03:38:48.803505Z[2020-03-05 03:38:48 +0000] [5] [INFO] Booting worker with pid: 5
2020-03-05T03:39:10.202062Z[2020-03-05 03:39:09 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:5)
2020-03-05T03:39:10.702339Z[2020-03-05 03:39:10 +0000] [5] [INFO] Worker exiting (pid: 5)
2020-03-05T03:39:18.801194Z[2020-03-05 03:39:18 +0000] [6] [INFO] Booting worker with pid: 6
Note the worker timeouts and reboots at the end of the logs. The fact that its a CRITICAL error makes me think it shouldn't be happing. Is this expected behavior? Is this a side effect of the Cloud Run machinery starting and stopping my service as requests come and go?
Cloud Run has scaled down one of your instances, and the gunicorn arbiter is considering it stalled.
You should add --timeout 0 to your gunicorn invocation to disable the worker timeout entirely, it's unnecessary for Cloud Run.
i was facing the error [11229] [CRITICAL] WORKER TIMEOUT (pid:11232) on heroku
i changed my Procfile to this
web: gunicorn --workers=3 app:app --timeout 200 --log-file -
and it fixed my problem by incresing the --timeout
Here's a working example of a Flask app on Cloud run. My guess is that your last line or the Decker file and the last part of your python file are the ones causing this behavior.
main.py
# main.py
#gcloud beta run services replace service.yaml
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello_world():
msg = "Hello World"
return msg
Dockerfile (the apt-get part is not needed)
# Use the official Python image.
# https://hub.docker.com/_/python
FROM python:3.7
# Install manually all the missing libraries
RUN apt-get update
RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils
# Install Python dependencies.
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . .
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app
then build using:
gcloud builds submit --tag gcr.io/[PROJECT]/[MY_SERVICE]
and deploy:
gcloud beta run deploy [MY_SERVICE] --image gcr.io/[PROJECT]/[MY_SERVICE] --region europe-west1 --platform managed
UPDATE
I've checked again the logs you've provided.
Getting this kind of warning/error is normal at the beginning after a new deployment as your old instances are not handling any requests but instead they are idle at that time until they are completely shut down.
Gunicorn also has a default timeout of 30s which matches with the time between the time of "Booting worker" and the time you see the error.
for those who are entering here and have this problem but with django (probably it will work the same) with gunicorn, supervisor and nginx, check your configuration in the gunicorn_start file or where you have the gunicorn parameters, in my case I have it like this, in the last line add the timeout
NAME="myapp" # Name of the application
DJANGODIR=/webapps/myapp # Django project directory
SOCKFILE=/webapps/myapp/run/gunicorn.sock # we will communicte using this unix socket
USER=root # the user to run as
GROUP=root # the group to run as
NUM_WORKERS=3 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=myapp.settings # which settings file should Django use
DJANGO_WSGI_MODULE=myapp.wsgi # WSGI module name
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source ../bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec ../bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=unix:$SOCKFILE \
--log-level=debug \
--log-file=- \
--timeout 120 #This

how to make my python-flask website available 24*7 using nginx and gunicorn

How to configure gunicorn to make sure my python-flask website is available 24*7 ?
The issue I am facing is: As soon I kill my terminal window, the website is no more reachable.
I am using rhel7.6 to host a website using python-flask.
I have configured nginx as the web server and gunicorn as the application server.
.
I will really appreciate if some one can help me in using/configuring gunicorn to make sure my website is available 24*7.
Please have some of my code as below:
[root#syed-dashboard-4 ~]# pwd
/root
[root#syed-dashboard-4 ~]#
[root#syed-dashboard-4 ~]# cat hello.py
#!/usr/bin/python
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello magentabox!"
#if __name__ == "__main__":
# app.run(host='10.145.29.23',port=5000)
[root#syed-dashboard-4 ~]#
[root#syed-dashboard-4 ~]# gunicorn hello:app
[2019-07-17 10:34:11 +0000] [9346] [INFO] Starting gunicorn 19.9.0
[2019-07-17 10:34:11 +0000] [9346] [INFO] Listening at: http://127.0.0.1:8000 (9346)
[2019-07-17 10:34:11 +0000] [9346] [INFO] Using worker: sync
[2019-07-17 10:34:11 +0000] [9351] [INFO] Booting worker with pid: 9351
I am pretty new to web development and as I mentioned as soon I close the terminal, the website is no more reachable. I can share the nginx configuration logs as well if that helps fixing my issue.
Thanks much.
You can use the supervisor. This is the professional way to run your server for 24*7.
Please add below file in your supervisor config to run.
[program:your_project_name]
command=/home/virtualenvpath/your_env/bin/gunicorn --log-level debug run_apiengine:main_app --bind 0.0.0.0:5006 --workers 5 --worker-class gevent
stdout_logfile=/home/your_path_to_log/supervisor_stdout.log
stderr_logfile=/home/your_path_to_log/supervisor_stderr.log
user=your_user
autostart=true
autorestart=true
environment=PYTHONPATH="$PYTHONPATH:/home/path_to_your_project";OAUTHLIB_INSECURE_TRANSPORT='1';
Configure this in supervisor it will run for 24*7. And whenever your machine restarts it will auto start.
On Linux, you can start your app in a tmux session that you detach once you started your server.
# Create a new tmux session
tmux new -s server
# Start your gunicorn server
cd /path/to/app
gunicorn hello:app
# Detach the current tmux session using Ctrl - B + D
You can close your terminal and your server will still be running.

Google App Engine deployment issue- main app not found

I am trying to deploy my app using Google App Engine. I have edited app.yaml to reflect the flexible environment and also gave all the app information. Below is the app.yaml file.
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app
runtime_config:
python_version: 3
Once the deployment is in progress, I am getting the following error
[2018-08-24 06:57:14 +0000] [1] [INFO] Starting gunicorn 19.7.1
[2018-08-24 06:57:14 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080 (1)
[2018-08-24 06:57:14 +0000] [1] [INFO] Using worker: sync
[2018-08-24 06:57:14 +0000] [7] [INFO] Booting worker with pid: 7
App Deployed
Failed to find application: 'main'
[2018-08-24 06:57:14 +0000] [7] [INFO] Worker exiting (pid: 7)
[2018-08-24 06:57:14 +0000] [1] [INFO] Shutting down: Master
[2018-08-24 06:57:14 +0000] [1] [INFO] Reason: App failed to load.
Please note that App Deployed is the line in my print statement. It is getting executed. But the deployment is getting failed
Thank you in advance
In your app.yaml, you're starting gunicorn with gunicorn -b :$PORT main:app. This tells it to look for the object app in the file main.py
The error you're getting comes from gunicorn and occurs when you have a main.py file, but it does not have an app object in it.
You probably want to set up a Flask app as follows:
from flask import Flask
app = Flask(__name__)
See a full example app here: https://cloud.google.com/appengine/docs/flexible/python/quickstart#hello_world_code_review
I have had the same issue.
Failed to find application: 'main'
The reason in my case was that I had a directory called also main in the root project directory.
drwxr-xr-x 2 user staff 64 Nov 30 10:12 main
-rw-r--r-- 1 user staff 1178 Nov 29 20:58 main.py
I guess gunicorn got confused.
The solution (worked for me): to rename main directory.
P.S. Just don't rename it to code directory. It will cause another issue with debugging in PyCharm. :)
My folder structure is looks llike this the main.py is not in the root directory how I can specify this in app.yaml file.
/app
|__main.py
app.yaml

Heroku app runs locally but gets H12 timeout error (uses a package)

Similar questions have been asked but the H12 seems to be caused by many things and none of the answers apply here. I have built python apps with heroku before but now I'm using a package structure per Miguel Grinberg's Flask Mega-Tutorial and I can't figure out what's wrong.
My app is python / Flask / SQLAlchemy with a Heroku postgres database. The app fully works locally. When I push to Heroku, I get an H12.
Here is my app structure:
rootappdirectory\
app\
static\
templates\
__init__.py
views.py
models.py
run.py
[plus flask / venv files]
run.py looks like this:
import os
from flask import Flask
from app import app
app.run()
And app/__init__.py looks like this:
(a bunch of imports)
app = Flask(__name__)
db = SQLAlchemy(app)
login_manager = LoginManager()
(a bunch of login_manager stuff)
from app import views, models
My Procfile is web: gunicorn run:app
I'm using the heroku database locally and remotely. The app works perfectly on my local machine (on 0.0.0.0:5000). But when I push to heroku and run the app, here's the logs:
2013-04-15T06:50:27.165532+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path=/favicon.ico host=floating-driftwood-6203.herokuapp.com fwd="24.6.48.240" dyno=web.1 connect=1ms service=30007ms status=503 bytes=0
2013-04-15T06:50:34.908756+00:00 app[web.1]: 2013-04-15 06:50:34 [2] [CRITICAL] WORKER TIMEOUT (pid:65)
2013-04-15T06:50:34.914436+00:00 app[web.1]: 2013-04-15 06:50:34 [2] [CRITICAL] WORKER TIMEOUT (pid:65)
2013-04-15T06:50:34.918114+00:00 app[web.1]: 2013-04-15 06:50:34 [66] [INFO] Booting worker with pid: 66
2013-04-15T06:50:35.083182+00:00 app[web.1]: * Running on http://127.0.0.1:5000/
2013-04-15T06:51:04.216671+00:00 app[web.1]: 2013-04-15 06:51:04 [2] [CRITICAL] WORKER TIMEOUT (pid:66)
2013-04-15T06:51:04.223440+00:00 app[web.1]: 2013-04-15 06:51:04 [2] [CRITICAL] WORKER TIMEOUT (pid:66)
2013-04-15T06:51:04.229350+00:00 app[web.1]: 2013-04-15 06:51:04 [67] [INFO] Booting worker with pid: 67
I've played with different options - at first I got a "connection in use" error which I fixed by going into debug=False, but frankly I'd rather be in debug=True! There seems to be a problem with the fact that my main app is not in the root directory?
Thanks in advance.
The issue is that run.py unguardedly calls app.run - this actually calls werkzeug.serving.run_simple which starts a sub-process to handle incoming requests ... which you don't want to do when running under gunicorn (since gunicorn will handle the process management for you).
Simply add an if __name__ == "__main__" guard before your app.run call and everything should work:
# run.py
if __name__ == "__main__":
app.run()

Categories

Resources