2013-04-24T06:24:58Z

The Flask Mega-Tutorial, Part XVIII: Deployment on the Heroku Cloud

(Great news! There is a new version of this tutorial!)

This is the eighteenth article in the series in which I document my experience writing web applications in Python using the Flask microframework.

The goal of the tutorial series is to develop a decently featured microblogging application that demonstrating total lack of originality I have decided to call microblog.

NOTE: This article was revised in September 2014 to be in sync with current versions of Python and Flask.

Here is an index of all the articles in the series that have been published to date:

In the previous article we explored traditional hosting options. We've looked at two actual examples of deployment to Linux servers, first to a CentOS system and later to the Raspberry Pi credit card sized computer. Those that are not used to administer a Linux system probably thought the amount of effort we had to put into the task was huge, and that surely there must be an easier way.

Today we will try to see if deploying to the cloud is the answer to the complexity problem.

But what does it mean to "deploy to the cloud"?

A cloud hosting provider offers a platform on which an application can run. All the developer needs to provide is the application, because the rest, which includes the hardware, operating system, scripting language interpreters and database, is managed by the service.

Sounds too good to be true, right?

We'll look at deploying to Heroku, one of the most popular cloud hosting services. I picked Heroku not only because it is popular, but also because it has a free service level, so we get to host our application without having to spend any money. If you want to find information about this type of services and what other providers are out there you can consult the Wikipedia page on platform as a service.

Hosting on Heroku

Heroku was one of the first platform as a service providers. It started as a hosting option for Ruby based applications, but then grew to support many other languages like Java, Node.js and our favorite, Python.

In essence, deploying a web application to Heroku requires just uploading the application using git (you'll see how that works in a moment). Heroku looks for a file called Procfile in the application's root directory for instructions on how to execute the application. For Python projects Heroku also expects a requirements.txt file that lists all the module dependencies that need to be installed.

After the application is uploaded you are essentially done. Heroku will do its magic and the application will be online within seconds. The amount of money you pay directly determines how much computing power you get for your application, so as your application gets more users you will need to buy more units of computing, which Heroku calls "dynos", and that is how you keep up with the load.

Ready to try Heroku? Let's get started!

Creating Heroku account

Before we can deploy to Heroku we need to have an account with them. So head over to heroku.com and create an account.

Once you are logged in you have access to a dashboard, where all your apps can be managed. We will not be using the dashboard much though, but it provides a nice view of your account.

Installing the Heroku client

Even though it is possible to manage applications from the Heroku web site to some extent, there are some things that can only be done from the command line, so we'll just do everything there.

Heroku offers a tool called the "Heroku client" that we'll use to create and manage our application. This tool is available for Windows, Mac OS X and Linux. If there is a Heroku toolbelt download for your platform then that's the easiest way to get the Heroku client tool installed.

The first thing we should do with the client tool is to login to our account:

$ heroku login

Heroku will prompt for your email address and your account password. The first time you login it will send your public SSH key to the Heroku servers.

Your authenticated status will be remembered in subsequent commands.

Git setup

The git tool is core to the deployment of apps to Heroku, so it must also be available. If you installed the Heroku toolbelt then you already have it as part of that installation.

To deploy to Heroku the application must be in a local git repository, first so let's get one set up:

$ git clone -b version-0.18 git://github.com/miguelgrinberg/microblog.git
$ cd microblog

Note that we are choosing a specific branch to be checked out, this is the branch that has the Heroku integration.

Creating a Heroku app

To create a new Heroku app you just use the create command from the root directory of the application:

$ heroku apps:create flask-microblog
Creating flask-microblog... done, stack is cedar
http://flask-microblog.herokuapp.com/ | git@heroku.com:flask-microblog.git

In addition to setting up a URL this command adds a git remote to our git repository that we will soon use to upload the application.

Of course the name flask-microblog is now taken by me, so make sure you use a different app name if you are doing this along.

Eliminating local file storage

Several of the functions of our application rely on writing data to disk files.

Unfortunately we have a tricky problem with this. Applications that run on Heroku are not supposed to write permanent files to disk, because Heroku uses a virtualized platform that does not remember data files, the file system is reset to a clean state that just contains the application script files each time a virtual worker is started. Essentially this means that the application can write temporary files to disk, but should be able to regenerate those files should they disappear. Also when two or more workers (dynos) are in use each gets its own virtual file system, so it is not possible to share files among them.

This is really bad news for us. For starters, it means we cannot use sqlite as a database, and our Whoosh full text search database will also fail to work, since it writes all its data to files. We also have the compiled translation files for Flask-Babel, which are generated when running the tr_compile.py script. And yet another area where there is problem is logging, we used to write our logfile.to the tmp folder and that is also not going to work when running on Heroku.

We have identified four major problems for which we need to try to find solutions.

For our first problem, the database, we'll migrate to Heroku's own database offering, which is based on PostgreSQL.

For the full text search functionality we don't have a readily available alternative. We could re-implement full text searches using PostgreSQL functionality, but that would require several changes to our application. It is a pity, but solving this problem now would be a huge distraction, so for now we'll disable full text searches when running under Heroku.

To support translations we are going to include the compiled translation files in the git repository, that way these files will be persistant in the file system.

Finally, since we can't write our own log file, we'll add our logs to the logger that Heroku uses, which is actually simple, since Heroku will add to its log anything that goes to stdout.

Creating a Heroku database

To create a database we use the Heroku client:

$ heroku addons:add heroku-postgresql:dev
Adding heroku-postgresql:dev on flask-microblog... done, v3 (free)
Attached as HEROKU_POSTGRESQL_ORANGE_URL
Database has been created and is available
 ! This database is empty. If upgrading, you can transfer
 ! data from another database with pgbackups:restore.
Use `heroku addons:docs heroku-postgresql:dev` to view documentation.
$ heroku pg:promote HEROKU_POSTGRESQL_ORANGE_URL
Promoting HEROKU_POSTGRESQL_ORANGE_URL to DATABASE_URL... done

Note that we are adding a development database, because that is the only database offering that is free. A production web server would need one of the production database options.

And how does our application know the details to connect to this database? Heroku publishes the URI to the database in the $DATABASE_URL environment variable. If you recall, we have modified our configuration to look for this variable in the previous hosting article, so the changes are already in place to connect with this database.

Disabling full text searches

To disable full text searches we need our application to be able to know if it is running under Heroku or not. For this we will add a custom environment variable, again using the Heroku client tool:

heroku config:set HEROKU=1

The HEROKU environment variable will now be set to 1 when our application runs inside the Heroku virtual platform.

Now it is easy to disable the full text search index. First we add a configuration variable (file config.py):

# Whoosh does not work on Heroku
WHOOSH_ENABLED = os.environ.get('HEROKU') is None

Then we suppress the creation of the full text database instance (file app/models.py):

from config import WHOOSH_ENABLED

enable_search = WHOOSH_ENABLED
if enable_search:
    import flask_whooshalchemy as whooshalchemy

# ...
if enable_search:
    whooshalchemy.whoosh_index(app, Post)

Precompiled Tranlations

This one is pretty easy. After running tr_compile.py we end up with a <language>.mo file for each <language>.po source file. All we need to do is add the mo files to the git repository, and then in the future we'll have to remember to update them. The mo file for Spanish is included in the branch of the git repository dedicated to this article.

Fixing the logging

Under Heroku, anything that is written to stdout is added to the Heroku application log. But logs written to a disk file will not be accessible. So on this platform we will suppress the file log and instead use a log that writes to stdout (file app/__init__.py):

if not app.debug and os.environ.get('HEROKU') is None:
    import logging
    from logging.handlers import RotatingFileHandler
    file_handler = RotatingFileHandler('tmp/microblog.log', 'a', 1 * 1024 * 1024, 10)
    file_handler.setLevel(logging.INFO)
    file_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
    app.logger.addHandler(file_handler)
    app.logger.setLevel(logging.INFO)
    app.logger.info('microblog startup')

if os.environ.get('HEROKU') is not None:
    import logging
    stream_handler = logging.StreamHandler()
    app.logger.addHandler(stream_handler)
    app.logger.setLevel(logging.INFO)
    app.logger.info('microblog startup')

The web server

Heroku does not provide a web server. Instead, it expects the application to start its own server on the port number given in environment variable $PORT.

We know the Flask web server is not good for production use because it is single process and single threaded, so we need a better server. The Heroku tutorial for Python suggests gunicorn, a pre-fork style web server written in Python, so that's the one we'll use.

For our local environment gunicorn installs as a regular python module into our virtual environment:

$ flask/bin/pip install gunicorn

To start this browser we need to provide a single argument that names the Python module that defines the application and the application object, both separated by a colon. Now for example, if we wanted to start a local gunicorn server with this module we would issue the following command:

$ flask/bin/gunicorn --log-file - app:app
2013-04-24 08:42:34 [31296] [INFO] Starting gunicorn 19.1.1
2013-04-24 08:42:34 [31296] [INFO] Listening at: http://127.0.0.1:8000 (31296)
2013-04-24 08:42:34 [31296] [INFO] Using worker: sync
2013-04-24 08:42:34 [31301] [INFO] Booting worker with pid: 31301

The requirements file

Soon we will be uploading our application to Heroku, but before we can do that we have to inform the server what dependencies the application needs to run. We created a requirements.txt file in the previous chapter, to simplify the installation of dependencies in a dedicated server, and the good news is that Heroku also imports dependencies from a requirements file.

The gunicorn web server needs to be added to the list, and so is the psycopg2 driver, which is required by SQLAlchemy to connect to PostgreSQL databases. The final requirements.txt file looks like this:

Babel==1.3
Flask==0.10.1
Flask-Babel==0.9
Flask-Login==0.2.11
Flask-Mail==0.9.0
Flask-OpenID==1.2.1
Flask-SQLAlchemy==2.0
Flask-WTF==0.10.2
Flask-WhooshAlchemy==0.56
Jinja2==2.7.3
MarkupSafe==0.23
SQLAlchemy==0.9.7
Tempita==0.5.2
WTForms==2.0.1
Werkzeug==0.9.6
Whoosh==2.6.0
blinker==1.3
coverage==3.7.1
decorator==3.4.0
flipflop==1.0
guess-language==0.2
gunicorn==19.1.1
itsdangerous==0.24
pbr==0.10.0
psycopg2==2.5.4
python-openid==2.2.5
pytz==2014.7
six==1.8.0
speaklater==1.3
sqlalchemy-migrate==0.9.2
sqlparse==0.1.11

Some of these modules will not be needed in the Heroku version of our application, but it really doesn't hurt to have extra stuff, to me it seems better to have a complete requirements list.

The Procfile

The last requirement is to tell Heroku how to run the application. For this Heroku requires a file called Procfile in the root folder of the application.

This file is extremely simple, it just defines process names and the commands associated with them (file Procfile):

web: gunicorn app:app
init: python db_create.py
upgrade: python db_upgrade.py

The web label is associated with the web server. Heroku expects this task and will use it to start our application.

The other two tasks, named init and upgrade are custom tasks that we will use to work with our application. The init task initializes our application by creating the database. The upgrade task is similar, but instead of creating the database from scratch it upgrades it to the latest migration.

Deploying the application

And now we have reached the most interesting part, where we push the application to our Heroku hosting account. This is actually pretty simple, we just use git to push the application:

$ git push heroku version-0.18
Counting objects: 307, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (168/168), done.
Writing objects: 100% (307/307), 165.57 KiB, done.
Total 307 (delta 142), reused 272 (delta 122)

-----> Python app detected
-----> No runtime.txt provided; assuming python-2.7.4.
-----> Preparing Python runtime (python-2.7.4)
-----> Installing Distribute (0.6.36)
-----> Installing Pip (1.3.1)
-----> Installing dependencies using Pip (1.3.1)
...
-----> Discovering process types
       Procfile declares types -> init, upgrade, web

-----> Compiled slug size: 29.6MB
-----> Launching... done, v6
       http://flask-microblog.herokuapp.com deployed to Heroku

To git@heroku.com:flask-microblog.git
 * [new branch]      master -> master

The label heroku that we used in the git push command was automatically registered with our git repository when we created our application with heroku create. To see how this remote repository is configured you can run git remote -v in the application folder.

The last argument, version-0.18 is the name of the branch we are deploying to Heroku. For your own applications, you will likely use master here.

The first time we push the application to Heroku we need to initialize the database and the translation files, and for that we can execute the init task that we included in our Procfile:

$ heroku run init
Running `init` attached to terminal... up, run.7671
/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/url.py:105: SADeprecationWarning: The SQLAlchemy PostgreSQL dialect has been renamed from 'postgres' to 'postgresql'. The new URL format is postgresql[+driver]://<user>:<pass>@<host>/<dbname>
  module = __import__('sqlalchemy.dialects.%s' % (dialect, )).dialects

The deprecation warning comes from SQLAlchemy, because it does not like that the URI starts with postgres:// instead of postgresql://. This URI comes from Heroku via the $DATABASE_URL environment variable, so we really don't have any control over this. Let's hope this continues to work for a long time.

Believe it or not, now the application is online. In my case, the application can be accessed at http://flask-microblog.herokuapp.com. For example, you can become my follower from my profile page. I'm not sure how long I'll leave it there, but feel free to give it a try if you can connect to it!

Updating the application

The time will come when an update needs to be deployed. This works in a similar way to the initial deployment. First the application is pushed from git. If your application is on the master branch, an update can be pushed with:

$ git push heroku master

Then the upgrade script is executed:

$ heroku run upgrade

Logging

If a problem occurs then it may be useful to see the logs. Recall that for the Heroku hosted version we are writing our logs to stdout which Heroku collects into its own logs.

To see the logs we use the Heroku client:

$ heroku logs

The above command will show all the logs, including Heroku ones. To only see application logs we can use this command:

$ heroku logs --source app

Things like stack traces and other application errors will appear in these app logs.

Is it worth it?

We've now seen what it takes to deploy to a cloud hosting service so we can now compare against the traditional hosting.

The simplicity aspect is easily won by cloud. At least for Heroku the deployment process was extremely simple. When deploying to a dedicated server or VPS there are a lot of administrative tasks that need to be done to prepare the system. Heroku takes care of all that and allows us to concentrate on our application.

The price is where it is harder to come to a conclusion. Cloud offerings are more expensive than dedicated servers, since you are not only paying for the server but also for the admin work. A pretty basic production service with Heroku that includes two dynos and the least expensive production database costs $85 per month at the time I'm writing this. On the other side, if you look hard you can find well provisioned VPS servers for abour $40 per year.

In the end, I think it all comes down to what is most important to you, time or money.

The End?

The updated application is available, as always, on my github page. Alternatively you can download it as a zip file below:

Download microblog 0.18.

With our application deployed in every possible way it feels like we are reaching the end of this journey.

I hope these articles were a useful introduction to the development of a real world web application project, and that the knowledge dump I've made over these eighteen articles motivates you to start your own project.

I'm not closing the door to more microblog articles. If and when an interesting topic comes to mind I will write more, but I expect the rate of updates from now on will slow down a bit. From time to time I may make small updates to the application that don't deserve a blog post, so you may want to watch the project on github to catch these.

I will continue blogging about topics related to web development and software in general, so I invite you to connect via Twitter or Facebook if you haven't done it yet, so that you find my future articles.

Thank you, again, for being a loyal reader.

Miguel

136 comments

  • #76 Andy said 2015-04-27T10:14:02Z

    Hi Miguel,

    Thanks for your help.

    For the second part , the push with heroku , I've found out the solution->

    You can create another branch

    git checkout -b master

    Once done, things are pushing fine :)

    Many thanks again for the tutorial and for the help.

  • #77 Huw said 2015-06-09T16:27:50Z

    What about the openid temp files. Openid seems to work just fine even without access to a temp folder on Heroku.

  • #78 Miguel Grinberg said 2015-06-09T18:04:06Z

    @Huw: Don't know. Maybe the OpenID extension regenerates these files with every log in attempt? It's more efficient to give it a temp folder and let it write what it wants to write.

  • #79 Ankit Kumar said 2015-07-01T19:46:04Z

    Can you please update this section of the blog? I am having trouble deploying to the heroku platform.

    Thanks a ton for the awesome flask tutorial..

  • #80 Miguel Grinberg said 2015-07-03T16:25:11Z

    @Ankit: what's the problem you are having?

  • #81 zealsham said 2015-10-17T23:14:05Z

    Wow!..very nice . seems heroku solved does problems now

  • #82 Mike K said 2016-01-01T01:13:46Z

    I tried adding to Heroku. However, I got the following errors at the 'Creating a Heroku database' stage. First, it said "WARNING: 'heroku addons:add' has been deprecated. Please use 'heroku addons:create' instead.

    OK, easy enough, I'll just use the new command. However the second error is:

    "That add-on plan is only available to select users."

  • #83 Miguel Grinberg said 2016-01-03T22:57:20Z

    @Mike: it looks like a small Postgres database of up to 10K rows is still free, according to Heroku's pricing page: https://www.heroku.com/pricing. Also note that a deprecated command still works. I wonder if the first time you've got a database, and then it is complaining that you are trying to add a second one.

  • #84 ecpost said 2016-01-06T04:26:19Z

    @Mike: They changed the name of the free one. Change that statement to:

    heroku addons:create heroku-postgresql:hobby-dev

    (changed "dev" to "hobby-dev", also changed the "add" to "create" to avoid the dep. warning)

  • #85 Ztomazin said 2016-01-22T18:13:48Z

    Miguel,

    This tutorial is amazing. I was successfully able to get my code deployed to Heroku, but it isn't working. I'm having this problem.

    2016-01-22T18:12:06.137445+00:00 app[web.1]: microblog startup 2016-01-22T18:12:06.154453+00:00 app[web.1]: microblog startup 2016-01-22T18:12:07.164928+00:00 app[web.1]: Traceback (most recent call last): 2016-01-22T18:12:07.164929+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app 2016-01-22T18:12:07.164931+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request 2016-01-22T18:12:07.164932+00:00 app[web.1]: rv = self.handle_user_exception(e) 2016-01-22T18:12:07.164930+00:00 app[web.1]: response = self.full_dispatch_request() 2016-01-22T18:12:07.164933+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception 2016-01-22T18:12:07.164938+00:00 app[web.1]: db.session.add(g.user) 2016-01-22T18:12:07.164939+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 150, in do 2016-01-22T18:12:07.164937+00:00 app[web.1]: rv = func() 2016-01-22T18:12:07.164924+00:00 app[web.1]: Exception on / [GET] 2016-01-22T18:12:07.164941+00:00 app[web.1]: raise exc.UnmappedInstanceError(instance) 2016-01-22T18:12:07.164940+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1490, in add 2016-01-22T18:12:07.164935+00:00 app[web.1]: rv = self.preprocess_request() 2016-01-22T18:12:07.164934+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1473, in full_dispatch_request 2016-01-22T18:12:07.164936+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1666, in preprocess_request 2016-01-22T18:12:07.164934+00:00 app[web.1]: reraise(exc_type, exc_value, tb) 2016-01-22T18:12:07.164942+00:00 app[web.1]: UnmappedInstanceError: Class 'werkzeug.local.LocalProxy' is not mapped 2016-01-22T18:12:07.164938+00:00 app[web.1]: File "/app/app/views.py", line 33, in before_request 2016-01-22T18:12:07.164940+00:00 app[web.1]: return getattr(self.registry(), name)(args, *kwargs)

    The app works on my local, but not there.

    Any ideas?

  • #86 Miguel Grinberg said 2016-01-27T15:42:46Z

    @Ztomazin: change "db.session.add(g.user)" to "db.session.add(g.user._get_current_object())". The g.user is a sort of magical Flask object that acts as a proxy to another object which is the real user. The _get_current_object() method returns the real object.

  • #87 Fernando said 2016-02-04T21:49:02Z

    Miguel, thank you very much for this series of tutorials. It has helped me alot and I published my first Flask App today. Thank you very much, I just wanted to say that!

  • #88 SirKal said 2016-02-25T04:37:55Z

    Hi Miguel, Great Blog. I learnt a lot. Can I create the db in the models file rather then in the init file? Are there any best practices on variable declarations. Thank You

  • #89 Miguel Grinberg said 2016-02-26T18:57:57Z

    @SirKal: consider that the database configuration is in the application, so you do need to have access to the application instance to do anything with the database. In reality you will find that operations on the database are done via separate command line tools, not really as part of running the application.

  • #90 Greg said 2016-04-08T18:14:38Z

    Thank you Miguel for this awesome tutorial. I went through all of it. I just bought your book too :)

  • #91 Arnaud said 2016-04-13T10:59:36Z

    Miguel, thanks a lot for these articles!

  • #92 rvan said 2016-05-11T08:18:30Z

    This series are fantastic! Miguel, you open the door to me for web development with Flask, really appreciate it!

  • #93 Mike T. said 2016-06-27T18:43:40Z

    I know it's been years, but I just used this to get my first Flask app going and there was a repo update 9 months ago so here goes:

    The procfile has this line: web: gunicorn runp-heroku:app which leads me to expect a runp-heroku.py but there is none.

    I just spent a lot of time figuring out that for it to work on heroku (these days anyway) I needed to put an "if name == 'main' above my app.run command. That could be because I skipped a few bits but I wonder if that's what "runp-heroku.py" had in it. I was getting errors like "address already in use" because apparently it would get run twice.

    Am I missing something?

    Thanks much for leaving this very informative tutorial up for all these years!

  • #94 Miguel Grinberg said 2016-07-14T18:15:53Z

    @Mike: the correct command is "gunicorn app:app". Seems I have a mistake in the github repository. I will correct it.

  • #95 Asif said 2016-07-23T18:33:27Z

    Hi Miguel,

    This is amazing. I learned so much from it. I am trying to run it on Heroku. It deploys without errors, but then when I go to the link, it says

    Application Error

    An error occurred in the application and your page could not be served. Please try again in a few moments.

    If you are the application owner, check your logs for details.

    Am i I supposed to delete the flask directory before uploading to Heroku? Could that be it?

    Thanks!

  • #96 Miguel Grinberg said 2016-07-28T05:39:17Z

    @Asif: the error message asks you to check the logs for details. Did you? What did you find there?

  • #97 JakeH said 2016-07-28T23:28:35Z

    @Miguel - I cannot seem to get this deployed correctly. I received no errors until the gunicorn testing step, where I got this traceback:

    [2016-07-28 17:15:02 -0600] [17955] [INFO] Starting gunicorn 19.6.0 [2016-07-28 17:15:02 -0600] [17955] [INFO] Listening at: http://127.0.0.1:8000 (17955) [2016-07-28 17:15:02 -0600] [17955] [INFO] Using worker: sync [2016-07-28 17:15:02 -0600] [17958] [INFO] Booting worker with pid: 17958 /Users/jacobhawkesworth/microblog/flask/lib/python2.7/site-packages/flask/exthook.py:71: ExtDeprecationWarning: Importing flask.ext.sqlalchemy is deprecated, use flask_sqlalchemy instead. .format(x=modname), ExtDeprecationWarning /Users/jacobhawkesworth/microblog/flask/lib/python2.7/site-packages/flask/exthook.py:71: ExtDeprecationWarning: Importing flask.ext.login is deprecated, use flask_login instead. .format(x=modname), ExtDeprecationWarning /Users/jacobhawkesworth/microblog/flask/lib/python2.7/site-packages/flask/exthook.py:71: ExtDeprecationWarning: Importing flask.ext.openid is deprecated, use flask_openid instead. .format(x=modname), ExtDeprecationWarning /Users/jacobhawkesworth/microblog/flask/lib/python2.7/site-packages/flask/exthook.py:71: ExtDeprecationWarning: Importing flask.ext.mail is deprecated, use flask_mail instead. .format(x=modname), ExtDeprecationWarning /Users/jacobhawkesworth/microblog/flask/lib/python2.7/site-packages/flask/exthook.py:71: ExtDeprecationWarning: Importing flask.ext.babel is deprecated, use flask_babel instead. .format(x=modname), ExtDeprecationWarning /Users/jacobhawkesworth/microblog/flask/lib/python2.7/site-packages/flask_sqlalchemy/init.py:800: UserWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning. warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning.') /Users/jacobhawkesworth/microblog/flask/lib/python2.7/site-packages/flask/exthook.py:71: ExtDeprecationWarning: Importing flask.ext.wtf is deprecated, use flask_wtf instead. .format(x=modname), ExtDeprecationWarning /Users/jacobhawkesworth/microblog/flask/lib/python2.7/site-packages/flask/exthook.py:71: ExtDeprecationWarning: Importing flask.ext.whooshalchemy is deprecated, use flask_whooshalchemy instead. .format(x=modname), ExtDeprecationWarning

    Because it appeared to be running nonetheless, I continued, and ran into no errors. But when I went to check my heroku link, I got an Application Error page. The heroku logs --source app command gave me only this line:

    ▸ Response code 500 (Internal Server Error)

    I also tried running on localhost, where I ran into a sqlalchemy.exc.OperationalError message about SQLite. The traceback from the Heroku log was:

    2016-07-28T04:32:00.923126+00:00 heroku[web.1]: Starting process with command gunicorn runp-heroku:app 2016-07-28T04:32:04.295186+00:00 app[web.1]: Traceback (most recent call last): 2016-07-28T04:32:04.295202+00:00 app[web.1]: File "/app/.heroku/python/bin/gunicorn", line 11, in 2016-07-28T04:32:04.295208+00:00 app[web.1]: sys.exit(run()) 2016-07-28T04:32:04.295210+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 74, in run 2016-07-28T04:32:04.295316+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 185, in run 2016-07-28T04:32:04.295317+00:00 app[web.1]: super(Application, self).run() 2016-07-28T04:32:04.295314+00:00 app[web.1]: WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run() 2016-07-28T04:32:04.295355+00:00 app[web.1]: Arbiter(self).run() 2016-07-28T04:32:04.295317+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 71, in run 2016-07-28T04:32:04.295559+00:00 app[web.1]: self.spawn_workers() 2016-07-28T04:32:04.295446+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 477, in manage_workers 2016-07-28T04:32:04.295561+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 542, in spawn_workers 2016-07-28T04:32:04.295388+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 169, in run 2016-07-28T04:32:04.295425+00:00 app[web.1]: self.manage_workers() 2016-07-28T04:32:04.295649+00:00 app[web.1]: time.sleep(0.1 * random.random()) 2016-07-28T04:32:04.295668+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 209, in handle_chld 2016-07-28T04:32:04.295735+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 459, in reap_workers 2016-07-28T04:32:04.295890+00:00 app[web.1]: gunicorn.errors.HaltServer: 2016-07-28T04:32:04.295734+00:00 app[web.1]: self.reap_workers() 2016-07-28T04:32:04.295809+00:00 app[web.1]: raise HaltServer(reason, self.WORKER_BOOT_ERROR) 2016-07-28T04:32:04.398398+00:00 heroku[web.1]: Process exited with status 1 2016-07-28T04:32:04.428712+00:00 heroku[web.1]: State changed from starting to crashed 2016-07-28T04:32:04.429792+00:00 heroku[web.1]: State changed from crashed to starting 2016-07-28T04:32:07.447060+00:00 heroku[web.1]: Starting process with command gunicorn runp-heroku:app 2016-07-28T04:32:10.143578+00:00 app[web.1]: Traceback (most recent call last): 2016-07-28T04:32:10.143598+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 74, in run 2016-07-28T04:32:10.143626+00:00 app[web.1]: WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run() 2016-07-28T04:32:10.143598+00:00 app[web.1]: sys.exit(run()) 2016-07-28T04:32:10.143627+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 185, in run 2016-07-28T04:32:10.143666+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 71, in run 2016-07-28T04:32:10.143694+00:00 app[web.1]: Arbiter(self).run() 2016-07-28T04:32:10.143664+00:00 app[web.1]: super(Application, self).run() 2016-07-28T04:32:10.143736+00:00 app[web.1]: self.manage_workers() 2016-07-28T04:32:10.143739+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 477, in manage_workers 2016-07-28T04:32:10.143593+00:00 app[web.1]: File "/app/.heroku/python/bin/gunicorn", line 11, in 2016-07-28T04:32:10.143934+00:00 app[web.1]: self.reap_workers() 2016-07-28T04:32:10.143813+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 542, in spawn_workers 2016-07-28T04:32:10.144049+00:00 app[web.1]: gunicorn.errors.HaltServer: 2016-07-28T04:32:10.144004+00:00 app[web.1]: raise HaltServer(reason, self.WORKER_BOOT_ERROR) 2016-07-28T04:32:10.143893+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 209, in handle_chld 2016-07-28T04:32:10.143697+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 169, in run 2016-07-28T04:32:10.143810+00:00 app[web.1]: self.spawn_workers() 2016-07-28T04:32:10.143890+00:00 app[web.1]: time.sleep(0.1 * random.random()) 2016-07-28T04:32:10.143937+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 459, in reap_workers 2016-07-28T04:32:10.204730+00:00 heroku[web.1]: Process exited with status 1 2016-07-28T04:32:10.225946+00:00 heroku[web.1]: State changed from starting to crashed 2016-07-28T04:33:59.524005+00:00 heroku[api]: Starting process with command python db_create.py by [my email] 2016-07-28T04:34:04.136948+00:00 heroku[run.2807]: Awaiting client 2016-07-28T04:34:04.181687+00:00 heroku[run.2807]: Starting process with command python db_create.py 2016-07-28T04:34:04.313814+00:00 heroku[run.2807]: State changed from starting to up 2016-07-28T04:34:09.126331+00:00 heroku[run.2807]: Process exited with status 0 2016-07-28T04:34:09.140255+00:00 heroku[run.2807]: State changed from up to complete 2016-07-28T04:35:22.454471+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=flask-microbloggery.herokuapp.com request_id=306700e9-4ac7-4fae-8ccb-efa99af13fd0 fwd="73.181.114.81" dyno= connect= service= status=503 bytes= 2016-07-28T04:35:23.365916+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=flask-microbloggery.herokuapp.com request_id=b64dccd9-8b8e-475a-be54-86886540fb83 fwd="73.181.114.81" dyno= connect= service= status=503 bytes=

    Any ideas would be appreciated!!

  • #98 Miguel Grinberg said 2016-08-06T16:17:30Z

    @JakeH: the first chunk of messages are all warnings. You can ignore them, but it would be better if you change your code so that these warnings do not show. I have actually updated the Flasky repository on GitHub to avoid the "flask.ext" warning for the deprecated import syntax.

    The gunicorn errors I'm not sure. I recommend that you make sure you can run the application locally first. The error is saying that the application isn't starting, so there must be something that prevents it from running.

  • #99 Jeff said 2016-08-12T12:21:23Z

    Hi Miguel,

    I'm having similar problems with @JakeH. I tried to run the web app with gunicorn locally, and it worked good. However, the one in heroku does not. Navgating to the page shows an application error. And reviewing the logs shows that the server did not start. These are the last few lines of the log (ran heroku logs --source app).

    2016-08-12T12:16:57.687860+00:00 app[web.1]: Traceback (most recent call last): 2016-08-12T12:16:57.687878+00:00 app[web.1]: File "/app/.heroku/python/bin/gunicorn", line 11, in 2016-08-12T12:16:57.687882+00:00 app[web.1]: sys.exit(run()) 2016-08-12T12:16:57.687883+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 74, in run 2016-08-12T12:16:57.687916+00:00 app[web.1]: WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run() 2016-08-12T12:16:57.687918+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 185, in run 2016-08-12T12:16:57.687957+00:00 app[web.1]: super(Application, self).run() 2016-08-12T12:16:57.687958+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 71, in run 2016-08-12T12:16:57.687986+00:00 app[web.1]: Arbiter(self).run() 2016-08-12T12:16:57.687987+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 169, in run 2016-08-12T12:16:57.688024+00:00 app[web.1]: self.manage_workers() 2016-08-12T12:16:57.688026+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 477, in manage_workers 2016-08-12T12:16:57.688093+00:00 app[web.1]: self.spawn_workers() 2016-08-12T12:16:57.688095+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 542, in spawn_workers 2016-08-12T12:16:57.688169+00:00 app[web.1]: time.sleep(0.1 * random.random()) 2016-08-12T12:16:57.688170+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 209, in handle_chld 2016-08-12T12:16:57.688212+00:00 app[web.1]: self.reap_workers() 2016-08-12T12:16:57.688278+00:00 app[web.1]: raise HaltServer(reason, self.WORKER_BOOT_ERROR) 2016-08-12T12:16:57.688214+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 459, in reap_workers 2016-08-12T12:16:57.688333+00:00 app[web.1]: gunicorn.errors.HaltServer:

  • #100 Miguel Grinberg said 2016-08-16T19:50:33Z

    @Jeff: anything else interesting in the log above the error? Gunicorn is unable to start for some reason.

Leave a Comment