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, but it has some limitations.
With synchronous Python web applications, the servers can be max out with 10,000 simultaneous connections, and the users can have a bad experience with blocked or waited 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 is Tornado 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 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 asynchronous feature Tornado 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
The Tornado web framework includes
RequestHandler(web application requests and responses).
AsyncHTTPclientfor server and client.
Asynchronous libraries like
IOStreamto implement protocols.
tornado.gen, that help developers to write asynchronous functions code native to Python
async defsyntax 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 build 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(
) or decorators (
) 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 save 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 provide a method
, that 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
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.
After executing the app.py script, open http://localhost:8888/ on your web browser.
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 the 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:
- How to Make a Game with Python?
- Python Data Structure
- Find Files in a Directory in Python
- Python Input Output (I/O) and Import
- Port Vulnerability Scanner in Python
- Python Data Types
- How to Run Python Scripts inside Docker Container?
- Best Way to Learn Python
- Python For Loop
- How do you Remove Duplicates from a Python List?