Some of you may have used the FriendFeed service that allows you to read and subscribe to the feeds or posts of your friends. It was developed as proprietary web engine. But later, it was open sourced after being acquired by Facebook.
One of the interesting features associated with the engine is that it is non-blocking in nature. This means that you can have special types of socket connects in your application and this will allow you connect to other applications.
While deploying the applications in Google App Engine (see the category ‘Google’ in this blog for details) we have seen that Google won’t allow us to include any code that creates socket connects. This is the reason why the following page (which is hosted in Google app engine server) return a server error when accessed:
http://php-app.aasisvinayak.com/sock.php
You can solve this issue by using Tornado Web server along with Google App Engine Framework.
Overview of the web server framework
As mentioned before it is a non-blocking server and is faster than most of other frameworks which follows similar architecture (Try webpy.org). Its code is licensed under Apache Licence and you can edit the code in order to suit your requirements. The web framework used by Tornado is the same one used by FriendFeed. Also, most of the tools used by the FriendFeed service are available in Tornado framework.
The most interesting characteristic is the non-blocking nature itself. A non-blocking socket (a concept introduced in Java 2 Standard Edition – J2SE- 1.4) allows you to have commutations between applications without blocking any process using sockets. In order words, you can bypass the restrictions placed by the app engine.
The framework is apt for real time web applications as it can allow so many simultaneous standing connections.
Installing the framework
Installation is very simple and straight forward. The only thing you need to worry is to make sure that the dependencies are met. Please note that you need python-dev, python-pycurl and python-simplejson for running this frame (which are available in the repository of your distribution).
Download the source file from here and issue the following commands:
$ tar xvfz tornado-0.2.tar.gz
$ cd ./tornado-0.2
$ python setup.py build
$ sudo python setup.py install
If your installation is successful, you will get an output similar to the one shown below:
Writing a sample application
Open you favourite text editor and add the following lines of code:
import tornado.*
class MainHandler(RequestHandler):
def get(self):
self.write("Your Text Message")
application = Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
my_http_server = HTTPServer(application)
my_http_server.listen(9900)
tornado.ioloop.IOLoop.instance().start()
Please note that sometimes you may need to give the absolute path to the class (if this code return an error). But this can be done easily by looking at the source tree.
As you can see, here you are writing a MainHandler handler class which prints a custom message. Then we use the HTTPServer() function within the main function and enable listening in port 9900. This means that the application will accept connections coming to 9900.
Modules
Some of the main modules employed are:
- web (which is the same framework used by the FriendFeed service)
- escape ( for XHTML, JSON, and URL encoding or decoding)
- database (which is essentially a wrapper around the MySQLdb so that MySQL can be managed easily)
- template (a Python based templating engine)
- httpclient (which is the non-blocking HTTP client)
- auth(for third party authentication services like Google OpenID or OAuth, Facebook Platform, Yahoo BBAuth and so on)
- locale ( for localization)
Demo
The source code we downloaded from the website also contains few demo applications.
You can go the directory and see the applications. If you want to run an application, say ‘helloworld’, then go to (cd) that directory and issue:
./helloworld.py
Now open your browser and go to localhost:8888 (as 8888 is the port used in that application) in order to see the application running:
Just like in Google App Engine, you can view the log information in the terminal itself.
You may try the ‘appengine’ application to see how the framework is integrating the Google App Engine Framework.
I’ll be posting one more tutorial on this topic where we will build an application using both the services. Please follow the blog updates so that you won’t miss it.




Join Techblog
Facebook Group
Read
Digg entries
Add techblog to
Google reader