What is Tornado in Python?

Posted in /  

What is Tornado in Python?
vinaykhatri

Vinay Khatri
Last updated on December 4, 2024

    Most of the Python web frameworks like Flask, Django, and Pyramid follow synchronous programming, which means their applications work on simple request and response architecture. The views written in synchronous Python web applications respond to the user when the user visits a specific URL.

    These days most applications use real-time updates, and Python synchronous-based web frameworks are not an ideal choice for such applications. Although using WSGI and other servers, multiple threads of request and response can be served for multiple users connected to the applications, and it has some limitations.

    With synchronous Python web applications, the servers can be maxed out with 10,000 simultaneous connections, and the users can have a bad experience with being blocked or waited for responses. To tackle such problems of synchronous web applications problems, developers prefer using Python asynchronous frameworks like Tornado.

    In this introductory article, we will discuss what Tornado is in Python and see how to get started with Tornado by writing a simple Tornado Hello World Program.

    What is Tornado in Python?

    Tornado is a Python web framework and asynchronous, non-blocking networking library. Because it follows asynchronous Python programming, it can handle serious web traffic. Tornado is not only a Python web framework it is also a web server framework.

    Developed by Bret Tylor for FriendFeed and later acquired by Facebook and released as an open-source framework in 2015. Tornado was designed to solve the C10K problem, in which a normal synchronous server gets maxed out when more than 10K users get connected to the server simultaneously, which leads to function blocking or waiting.

    But with Tornado's asynchronous feature, it can handle more than 10k users simultaneously, which makes it a perfect Python framework for real-time update applications. The complete Python Tornado is a composition of four major Tools

    1. The Tornado web framework includes RequestHandler (web application requests and responses).
    2. HTTPServer and AsyncHTTPclient for server and client.
    3. Asynchronous libraries like IOLoop and IOStream to implement protocols.
    4. Coroutine library tornado.gen , that help developers to write asynchronous functions code native to Python async def syntax introduced in Python 3.5.

    Features of Tornado

    1. Minimal and lightweight

    Like Flask, Tornado is a lightweight and minimal framework. Despite its few dependencies, the application built on Tornado can be scaled as big as Django, but unlike Django, it does not force its user to follow a monolithic pattern.

    2. Asynchronous Programming

    Tornado can use native coroutines( async await ) or decorators ( def yield ) to implement asynchronous programming using Python. Because of its asynchronous nature, it can have thousands of open connections to serve real-time updates.

    3. Secure Cookies

    cookies are the not safest approach to saving client-side data. That's why Tornado provides secure cookies to prevent forgery. With Tornado secure cookies, the user can set a secret key to the cookies created by the application.

    4. User Authentication

    Like Django, Tornado comes with built-in User authentication, so as a developer, we do not need to reinvent the wheel again.

    5. Social Media Authentication

    Tornado also provides a method tornado.auth , which can handle authentication and authorization from different social media platforms, including Google/Gmail, Facebook, Twitter, and FriendFeed.

    6. CSRF protection

    Cross-Site Request Forgery (CSRF) is one of the most common threats to a personalized web application. But with tornado XSRF protection, we do not have to worry about it.

    Get started with Tornado

    Like all other Python frameworks, we need to install Tornado for our Python environment before writing our first Tornado web application. To install the Python Tornado framework, we can use Python's pip install command.

    pip install tornado

    As Tornado is a Asynchronous Python Framework, to run the tornado application on your system Python 3.5 or above version must be installed on your operating systsm. Because asynchronyzation introduced in Python 3.5.

    Now let's write our First Tornado Hello World program. Like Flask, we can start writing our tornado web application with a single Python app.py file. #app.py

    import tornado.ioloop
    import tornado.web
    
    class IndexPageHandler(tornado.web.RequestHandler):
    
        #get request
        def get(self):
            self.write("<h1>Hello world! Welcome to Tornado</h1>")
    
        if __name__ == "__main__":
        application = tornado.web.Application([
                                       (r"/", IndexPageHandler),
                                       ])
    
        application.listen(8888) # run app on port 8888
        tornado.ioloop.IOLoop.current().start()

    Now execute the app.py on the terminal or command prompt.

    python app.py

    After executing the app.py script, open http://localhost:8888/ on your web browser.

    Conclusion

    Now let's conclude our introductory article on Python's Tornado Framework. Tornado is a lightweight Python web framework and web server that use asynchronous programming to write Python web application to solve problem like C10K. Tornado is as minimal as Flask and as secure and scalable as Django. It is one of the best Python frameworks that can build the perfect application to handle heavy traffic.

    To know more about Tornado, check out its official documentation .

    People are also reading:

    FAQs


    Tornado is a web framework and an asynchronous network library. It leverages a non-blocking network-io so that it can handle thousands of active connections.

    You can use the 'pip install tornado' command to install Tornado.

    Yes, Tornado is an asynchronous network library.

    Tornado is a web server framework, along web application framework developed in Python.

    A company called FriendFeed, which was later acquired by Meta in 2009, developed Tornado.

    Leave a Comment on this Post

    0 Comments