Python Tornado
Asynchronous Programming
Blocking Issue
Web Framework
Non-blocking IO

Python Tornado - Asynchronous Request is blocking

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Overview of Python Tornado

Tornado is a Python web framework and asynchronous networking library originally developed by FriendFeed. It's designed to handle thousands of simultaneous network connections and solve the C10k problem, where traditional frameworks struggle. The Tornado framework provides mechanisms for building web applications, dealing with non-blocking network operations, and implementing real-time services.

Asynchronous Requests in Tornado

One of the primary features of Tornado is its ability to handle asynchronous input/output operations. It uses a single-threaded, event-driven model that allows for handling multiple connections at once without blocking. The tornado.ioloop module, along with Future and coroutine, provides an easy way to write asynchronous code that feels synchronous.

How Non-blocking I/O Works

Non-blocking I/O allows the server to process other tasks while waiting for I/O operations to complete, whereas blocking I/O would halt the server until the operation is finished. In Tornado:

  • IOLoop: It's the core event loop in Tornado where asynchronous events are handled.
  • Coroutines: Using Python's async and await keywords, coroutines enable writing code that behaves as if it was blocking (sequential).
  • Futures and Callbacks: These provide a way to work with asynchronous results.

Common Misconception: Asynchronous Request is Blocking

While Tornado provides non-blocking operations, a common mistake occurs when developers inadvertently introduce blocking operations into their request handlers. Blocking operations halt the IOLoop and can significantly degrade performance.

Examples of Blocking Code

  1. Synchronous Database Calls: If a request handler uses a synchronous database client, the handler is effectively blocking the IOLoop from processing other requests.
  2. Time-consuming Computations: Operations that involve complex calculations without yielding control back to the IOLoop can block asynchronous requests.
  3. Thread or Blocking Wait: Using Python's native threading or blocking calls like time.sleep() in the request handler can cause blocking.

Convert Blocking Operations to Non-blocking

  1. Asynchronous Libraries: Use asynchronous libraries for database operations, such as aiopg for PostgreSQL or motor for MongoDB.
  • Non-blocking architecture: Leverage Tornado's non-blocking architecture to maximize server throughput.
  • Async Libraries: Use libraries that support Tornado's asynchronous models.
  • Resource Management: Manage CPU-bound tasks and I/O-bound tasks appropriately to maintain responsiveness.
  • Futures: Represent promises to complete operations and results of these operations. Used for asynchronous tasks.
  • Callbacks: Instead of blocking and waiting for operations to complete, notify when a task is done via callbacks.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.