A really simple guide on how to run tornado on heroku [1]

Table of contents

Recently, I've been experimenting with the Tornado web framework. Tornado is a python web application framework that is asynchronous in nature. It can handle massive simultaneous connections and really fast!

So today, I introduce you to the Tornado framework in my own way. I also would guide you on how to host your Tornado app on the heroku cloud service. However, it is worthy to note that you must have a little experience coding in a language in general, or in python specifically. If you are on this game with me, why don't you hop right in and let's get started!

Requirements

  • Python

  • Visual Studio Code (or any other IDE that provides git integration)

  • Tornado

You must have installed on your system the latest version of python. You can download the latest version of python for your operating system at %[python.org/downloads/]. Make sure the python interpreter and pip are installed and running perfectly on your system.

Next, download and install Visual Studio Code (code.visualstudio.com). personally, I love Visual Studio Code because of its nice integration with git. other popular IDEs and text editors would still serve just fine.

Now, open a command shell (or command prompt for windows users) and type in the following:

pip install tornado

This command tells pip (a python package manager) to download and install tornado from the python repository site PYPI (pypi.org). Once it is done installing and cleaning up. Fire on your Visual Studio Code and type in the following:

#!usr/bin/env python


import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, World!")

def make_app():
    return tornado.web.Application([
    (r"/", MainHandler),
    ])

In the above program, you imported the tornado web and IOLoop modules.

import tornado.ioloop
import tornado.web

Then you declared the main handler class which inherits the tornado.web.RequestHandler class:

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, World!")

Next, you define the route configuration in the make_app() function:

def make_app():
    return tornado.web.Application([
    (r"/", MainHandler),
    ])

Here, the route r"/" is assigned to the MainHandler class, which would return a "Hello, World!" to the browser.

What's next? We need to make our program to run by calling the tornado non-blocking httpserver. Here is how we do it:

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

Here, we assigned our routing function make_app() to app. We made our app to listen on port 8888. Finally, we invoke the tornado.ioloop to start up our app in a non-blocking manner.

Now, save this as tornado_app.py and run it as follows:

python tornado_app.py

You should be able to have something such as this:

Untitled 9.jpg