Skip to content

How to Fix Python Tornado Async I/O Loop Error

DodaTech Updated 2026-06-24 1 min read

In this tutorial, you'll learn about How to Fix Python Tornado Async I/O Loop Error. We cover key concepts, practical examples, and best practices.

Tornado raises RuntimeError when the I/O loop is already running, the wrong decorator is used, or the event loop is closed before callbacks complete.

The Wrong Way

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello")

app = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    app.listen(8000)
    tornado.ioloop.IOLoop.current().start()

Calling IOLoop.current().start() twice crashes:

RuntimeError: IOLoop is already running

The Right Way

import tornado.ioloop
import tornado.web
import asyncio

class MainHandler(tornado.web.RequestHandler):
    async def get(self):
        self.write("Hello, Tornado")

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8000)
    tornado.ioloop.IOLoop.current().start()

Output:

Server running on http://localhost:8000

Prevention Tips

  • Use app.listen() before starting the I/O loop.
  • Use async def for handler methods that await coroutines.
  • Use tornado.platform.asyncio.AsyncIOMainLoop for asyncio integration.
  • Call IOLoop.current().start() only once.
  • Use IOLoop.run_sync() for running async code from sync context.

Common Mistakes with tornado async

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. Forgetting deriving (Show, Eq) on custom data types needed for debugging

These mistakes appear frequently in real-world PYTHON code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between Tornado and FastAPI?

Tornado is an older async framework focused on long-polling and WebSockets. FastAPI is a modern framework with automatic OpenAPI docs. Tornado excels at real-time applications.

How do I use Tornado with async/await?

Tornado supports async def natively. Use async def for handler methods. Await coroutines inside. Tornado's event loop is compatible with Python's asyncio in recent versions.

Why does Tornado ignore my error handler?

Register error handlers before routes. Use app.add_handlers(".*", ...) for catch-all patterns. Set default_handler_class for custom 404 responses.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro