Inside Tornado’s IOLoop: Callbacks, Futures & Coroutines Explained

This article provides a detailed examination of Tornado’s IOLoop core, explaining its callback handling, timer management, I/O event loop, and the pivotal role of Future objects, while also demystifying the implementation of gen.coroutine and offering practical code examples to illustrate asynchronous execution.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Inside Tornado’s IOLoop: Callbacks, Futures & Coroutines Explained

IOLoop Core Overview

The IOLoop class is the core scheduler of Tornado, responsible for managing callbacks, timeouts and I/O events within a while True loop.

Callback Mechanism

Callbacks are stored in self._callbacks and executed each iteration. Methods such as add_callback and add_future schedule functions to run later, wrapping them with stack_context.wrap when needed.

Timer Management

Timeouts are kept in self._timeouts. The loop calculates the nearest timeout and uses it as the poll timeout for the underlying poller, ensuring timely execution of scheduled callbacks.

I/O Event Loop

File descriptors are registered with add_handler(fd, handler, events). The poller monitors these descriptors and invokes the associated handler when events occur.

Future Object

Tornado’s Future (or TracebackFuture) represents a value that will become available later. It provides add_done_callback to register callbacks and set_result or set_exc_info to complete the future.

gen.coroutine Implementation

The @gen.coroutine decorator wraps a generator function, creates a Future, runs the generator, and drives it forward by sending results back whenever a yielded Future is resolved. The internal _make_coroutine_wrapper and Runner classes handle this orchestration.

Practical Code Examples

Examples demonstrate adding a future, using add_timeout to simulate I/O delay, and writing a coroutine that yields futures to compute a sum asynchronously.

Overall, the IOLoop combines callbacks, timers, and I/O events to provide a non‑blocking execution model that underpins Tornado’s higher‑level features such as gen.coroutine, AsyncHTTPClient, and IOStream.

IOLoop execution output
IOLoop execution output
gen.coroutine example output
gen.coroutine example output
Coroutine sum example output
Coroutine sum example output
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonAsynchronoustornadocoroutineFutureIOLoop
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.