Tornado is a web framework implemented in Python, designed for asynchronous network programming and non-blocking I/O. It can handle a very large number of simultaneous connections, making it especially suitable for applications requiring high concurrency.
Professionals particularly use Tornado for building chat applications, live dashboards, and other real-time services where maintaining open connections is essential. Its architecture allows efficient handling of multiple requests at once without the need for multiple threads, resulting in improved performance and scalability.
For businesses that require high-concurrency solutions (where many requests must be processed simultaneously), Tornado offers both speed and flexibility in a developer-friendly environment.
In this blog, we will explore what the Tornado framework is, its key features, how it works, and its common use cases. Let's get started!
Tornado is a Python web framework that is used for building fast and scalable web applications. It works asynchronously, which means that it can handle many users at the same time without slowing down. It is used for real-time applications like chat apps, live updates, and services that need high performance and speed.
One of the earliest real-time web framework projects is the Tornado framework, which was created in 2009 by FriendFeed to handle thousands of concurrent connections through web applications. Facebook purchased FriendFeed and has since made this an open-source project. With its non-blocking I/O and asynchronous networking capabilities, Tornado has grown in popularity for building scalable, high-performance service applications such as chat applications and live updates.
Tornado comes equipped with advanced capabilities that make it a popular choice among developers. This framework is especially valued in scenarios requiring real-time communication and efficient handling of multiple concurrent connections. The following are some of its key features:
Read Also- Top Python Libraries
When we start a web page that is built with Tornado, do you ever think about what happens behind the scenes? For a beginner, it will help you to understand this when looking at code to see how to put these ideas into your own project. Let me explain to you its working in a step-by-step process:
This is where you define how your app will respond to a user request.
import tornado.web class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, Tornado!") |
Now you have to map the URL to your handler.
def make_app(): return tornado.web.Application([ (r"/", MainHandler), ]) |
When a user visits /, Tornado uses MainHandler.
Run the application on a specific port.
if __name__ == "__main__": app = make_app() app.listen(8888) |
This is the core of Tornado.
import tornado.ioloop tornado.ioloop.IOLoop.current().start() |
The event loop keeps running and listens for incoming requests.
When a user opens the browser and goes to:
| http://localhost:8888/ |
Tornado receives this request in the event loop.
Sometimes your app needs to wait, like when fetching data from a database or calling an API. But instead of just sitting idle, Tornado does something smart.
import asyncio class AsyncHandler(tornado.web.RequestHandler): async def get(self): await asyncio.sleep(2) # Simulating delay self.write("Async Response") |
In this example, Tornado waits for 2 seconds, but during that time, it does not stop working. It keeps handling other users' requests in the background.
Once the task is done, Tornado returns the result instantly and the user sees the response in the browser.
Read Also- Python Tutorial
Creating a Tornado project is a straightforward process and it involves a few steps to get started. Before proceeding, it is important to understand its prerequisites.
Here are the basic things you must have before getting started with Torando.
Install Tornado using pip:
| pip install torando |
Then you will create a file named app.py and add the following code:
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), ]) if __name__ == "__main__": app = make_app() app.listen(8888) tornado.ioloop.IOLoop.current().start() |
Now, run your Tornado server by executing the following command:
| python app.py |
Open a web browser and navigate to http://localhost:8888. You will see "Hello, world" displayed on the page.
When you are choosing a technology to build a web application, it is important to understand how different frameworks and platforms compare. Tornado, Django, Flask and Node.js are all popular choices, but they are designed with different goals in mind. Below is their brief differentiation for your understanding:
| Feature | Tornado | Django | Flask | Node.js |
| What it is | A fast Python framework for real-time apps | A full-featured Python framework | A lightweight Python framework | A JavaScript runtime for backend |
| Best for | Real-time apps (chat, live updates) | Large, complex websites | Small to medium projects | Real-time apps & scalable systems |
| Ease of use | Moderate (needs async understanding) | Easy (many built-in features) | Very easy (simple and flexible) | Moderate (async concepts needed) |
| Performance | Very fast (non-blocking) | Good but slower than Tornado | Moderate | Very fast (event-driven) |
| Built-in features | Minimal | Many (auth, admin, ORM) | Very few | Minimal (use packages) |
| Flexibility | High | Less flexible (structured) | Very high | Very high |
| Learning curve | Medium | Medium | Low | Medium |
| Language | Python | Python | Python | JavaScript |
| Use case example | Live chat app | E-commerce website | Simple blog or API | Streaming apps, APIs |
Tornado is a Python web framework that is great at handling many users at the same time. It is especially useful when your application needs to send or receive data quickly and continuously, without making users wait. Here are some of its common use cases:
Tornado is suitable for applications that require real-time updates, such as chat applications, live feeds and notifications. Its support for WebSockets ensures that data can be pushed to clients instantly, which provides a seamless user experience.
Code example:
import tornado.ioloop import tornado.web import tornado.websocket clients = set() class ChatHandler(tornado.websocket.WebSocketHandler): def open(self): clients.add(self) print("New client connected") def on_message(self, message): for client in clients: client.write_message(message) def on_close(self): clients.remove(self) print("Client disconnected") app = tornado.web.Application([ (r"/chat", ChatHandler), ]) if __name__ == "__main__": app.listen(8888) print("Chat server running on http://localhost:8888/chat") tornado.ioloop.IOLoop.current().start() |
Tornado's in-built WebSocket support makes it ideal for applications that need bi-directional communication between the server and the client. This is particularly useful for collaborative tools, online games and real-time data dashboards.
Code example:
import tornado.ioloop import tornado.web import tornado.websocket class EchoWebSocket(tornado.websocket.WebSocketHandler): def open(self): print("WebSocket opened") def on_message(self, message): self.write_message(f"Echo: {message}") def on_close(self): print("WebSocket closed") app = tornado.web.Application([ (r"/ws", EchoWebSocket), ]) if __name__ == "__main__": app.listen(8888) print("WebSocket server running on ws://localhost:8888/ws") tornado.ioloop.IOLoop.current().start() |
Applications that require long-lived connections, such as multiplayer online games or live streaming services, benefit from Tornado's ability to maintain numerous open connections efficiently. This capability always makes sure that users remain connected and receive updates without interruption.
Code example:
import tornado.ioloop import tornado.web import asyncio class StreamHandler(tornado.web.RequestHandler): async def get(self): self.set_header("Content-Type", "text/plain") for i in range(5): self.write(f"Update {i}\n") await self.flush() await asyncio.sleep(1) app = tornado.web.Application([ (r"/stream", StreamHandler), ]) if __name__ == "__main__": app.listen(8888) print("Streaming server running on http://localhost:8888/stream") tornado.ioloop.IOLoop.current().start() |
This framework can be used to build scalable microservices that need to handle high volumes of network traffic. Its lightweight and efficient design makes it an excellent choice for creating small, independent services that work together to form a larger application.
Code example:
import tornado.ioloop import tornado.web import asyncio class ApiHandler(tornado.web.RequestHandler): async def get(self): await asyncio.sleep(1) # Simulate async DB call self.write({"status": "success", "data": "Hello from Tornado microservice"}) app = tornado.web.Application([ (r"/api", ApiHandler), ]) if __name__ == "__main__": app.listen(8888) print("API server running on http://localhost:8888/api") tornado.ioloop.IOLoop.current().start() |
Due to its high efficiency, it can be used to build proxies and load balancers that distribute network traffic. This use case is essential for applications that manage large amounts of traffic across multiple servers to ensure reliability and performance.
Code example:
import tornado.ioloop import tornado.web import tornado.httpclient class ProxyHandler(tornado.web.RequestHandler): async def get(self): client = tornado.httpclient.AsyncHTTPClient() response = await client.fetch("http://httpbin.org/get") self.write(response.body) app = tornado.web.Application([ (r"/proxy", ProxyHandler), ]) if __name__ == "__main__": app.listen(8888) print("Proxy server running on http://localhost:8888/proxy") tornado.ioloop.IOLoop.current().start() |
Read Also- Python Interview Questions and Answers
Tornado is a fast and scalable Python web framework designed for handling many simultaneous connections. It is ideal for real-time applications like chat systems and live updates due to its efficient asynchronous and non-blocking architecture.
Here are some of its advantages:
Though Tornado has a fast and scalable asynchronous architecture, the way it works can be difficult for people to understand. Since Tornado's emphasis is on performance instead of simplicity, it can also be harder for new developers to find help when developing or debugging.
Tornado is a high-performance and asynchronous networking library that is designed to scale to tens of thousands of open connections. Following best practices ensures your application remains responsive and scalable.
To wrap up, Tornado is an effective way to create real-time web apps that are fast, scalable and built around the idea of asynchronous code. Tornado's ability to support numerous concurrent users, it can be perfect for modern applications such as chat applications, live updates and streaming services through its integration of WebSockets.
Although Tornado may require more time to learn than some other frameworks and has a smaller development community than other frameworks, the speed and scale of Tornado make it a fantastic alternative for developers needing high-performance solutions. In conclusion, Tornado is an excellent choice for developing real-time apps when real-time communication and performance are critical.
If you want to build high speed, real time and scalable apps, Tornado is a great choice.
Tornado uses a non-blocking I/O model and an event loop, which allows it to handle multiple requests simultaneously without waiting for each task to complete.
Yes, Tornado can be used to build REST APIs by defining request handlers for different HTTP methods like GET, POST, PUT and DELETE.
Tornado is designed for high concurrency and can efficiently handle thousands of simultaneous connections, which makes it suitable for high-traffic applications.