Running Tornado Web Server On Heroku

Heroku Logo

Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed. By using non-blocking network I/O, Tornado can scale to tens of thousands of open connections, making it ideal for long polling, WebSockets, and other applications that require a long-lived connection to each user. We can running the web server on Heroku cloud application platform that support Ruby, Node.js, Clojure, Java, Python and Scala.

In this article we will create a simple Hello World application. First of all, install the Heroku Toolbelt on your local workstation. This ensures that you have access to the Heroku command-line client, Foreman, and the Git revision control system. You can download example source at the bottom of this article.

Creating The Files

First of all, we need to create our Python repository with at least three files to work with Heroku. Heroku recognizes Python applications by the existence of a requirements.txt file in the root of a repository. Procfile will let us run our command. In this example we will use main.py that contains our Python code.

requirements.txt, this will install Tornado version 3.1.1

tornado==3.1.1

main.py, our simple Python Hello World application

import os
import tornado.httpserver
import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello world")

def main():
    application = tornado.web.Application([
        (r"/", MainHandler),
    ])
    http_server = tornado.httpserver.HTTPServer(application)
    port = int(os.environ.get("PORT", 5000))
    http_server.listen(port)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main()

Procfile, this will run the main() function in the main.py.

web: python main.py

Deploying Application to Heroku

Now that we’ve written and tested our application, we need to store the project in a Git repository. Open your console command line or command prompt for Windows and type this command.

$ git init
$ git add .
$ git commit -m "hello world init"

We will need create a space on Heroku to store our git repository.

$ heroku create
Creating arcane-earth-7310...done, stack is cedar
http://arcane-earth-7310.herokuapp.com/ | git@heroku:arcane-earth-7310.git

Next, we will need to push our application to Heroku.

$ git push heroku master
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 593 bytes, done.
Total 5 (delta 0), reused 0 (delta 0)

-----> 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)
       ...
       Successfully installed tornado
       Cleaning up...
-----> Discovering process types
       Procfile declares types -> web
-----> Compiled slug size is 27.5MB
-----> Launching... done, v3
       http://arcane-earth-7310.herokuapp.com deployed to Heroku

To git@heroku.com:arcane-earth-7310.git
 * [new branch]      master -> master

That’s it, we are done with Tornado on Heroku. Try to visit the link with your browser, in the example above http://arcane-earth-7310.herokuapp.com/ and you will see a Hello World page.

Download source file

Source:
https://devcenter.heroku.com/articles/getting-started-with-python

3 comments on “Running Tornado Web Server On Heroku

Create Comment

Your email address will not be published. Required fields are marked *