Need advice about which tool to choose?Ask the StackShare community!
Tornado vs asyncio: What are the differences?
Introduction
In this article, we will explore the key differences between Tornado and asyncio, two popular frameworks used for asynchronous programming in Python.
Execution Model: Tornado is an event-driven framework, while asyncio is based on coroutines. Tornado uses an event loop to manage events and callbacks, allowing it to handle a large number of simultaneous connections efficiently. On the other hand, asyncio employs coroutines, which are lightweight functions that can be paused and resumed, to achieve asynchronous execution.
Compatibility: Tornado is compatible with both Python 2 and Python 3, making it suitable for a wider range of projects. asyncio, on the other hand, is specifically designed for Python 3.4 and above, taking advantage of the language improvements introduced in later versions.
Code Structure: Tornado follows a more traditional approach with explicit callbacks and the use of decorator-based syntax for defining request handlers. It provides a comprehensive set of features for building web applications, including routing, template rendering, and WebSocket support. asyncio, on the other hand, provides a more lightweight and flexible framework for asynchronous programming, allowing developers to create custom event loops and use coroutines in a more modular manner.
Community and Support: Tornado has been around for a longer time and has a larger community of users and contributors. It has been extensively used in production environments and has a well-documented API. asyncio, being a relatively newer framework, is still evolving and may have fewer third-party libraries and resources available.
Concurrency Models: Tornado uses a single-threaded, non-blocking I/O approach to achieve high concurrency. It achieves this by using asynchronous networking and non-blocking file I/O operations. asyncio, on the other hand, provides a more granular control over concurrency through the use of coroutines and can handle more complex concurrency patterns, such as concurrent HTTP requests.
In summary, the key differences between Tornado and asyncio lie in their execution models, compatibility, code structure, community support, and concurrency models. These differences make them suitable for different types of projects and use cases.
I want to use Redis non-blocking with Tornado. How I should use the Redis code using tornadis to achieve max number of connections. I had used the tornadis and with WebSocket handler, I inherited the Redis based pub/sub client inside a class and in the main loop we created the instance of a class. but it decreases the numbr of connections with a huge amount. how we should configure?
If there is a way to connect to Redis via UNIX sockets instead of TCP then it worth it to switch over. We had a busy e-commerce site with heavy Redis usage and we saw a 4-5x(!) performance improvement during testing situations with a high number of concurrent requests.
I know that the probability these days is rather low to have such a scenario where you have Redis and the app on the same host (and thus the ability to connect over UNIX sockets). But if that's the case, it's definitely worth it.
Pros of asyncio
- Cooperative Multitasking4
- I/O Wait4
- Network Call3
- I/O bound computation2
Pros of Tornado
- Open source37
- So fast31
- Great for microservices architecture27
- Websockets20
- Simple17
- Asynchronous14
- Python11
- Lightweight7
- Handles well persistent connexions3
Sign up to add or upvote prosMake informed product decisions
Cons of asyncio
Cons of Tornado
- Event loop is complicated2