AboutBlogContact
Backend EngineeringOctober 5, 2004 2 min read 147Updated: June 22, 2026

Event-Driven Python: Mastering Twisted and Deferreds (2004)

AunimedaAunimeda
📋 Table of Contents

Event-Driven Python: Mastering Twisted and Deferreds

Threads are a trap. They're heavy, they have race conditions, and they don't scale. If you want to build a high-performance chat server or a web crawler in Python, you need Twisted. It's the "Matrix" of networking frameworks: it's everywhere once you know how to see it.

The Reactor Loop

In Twisted, everything revolves around the reactor. You don't call recv() and wait for data. Instead, you register a callback that gets triggered when data arrives.

from twisted.internet import protocol, reactor

class Echo(protocol.Protocol):
    def dataReceived(self, data):
        self.transport.write(data)

class EchoFactory(protocol.Factory):
    def buildProtocol(self, addr):
        return Echo()

reactor.listenTCP(8000, EchoFactory())
reactor.run()

Understanding Deferreds

The hardest part of Twisted for newcomers is the Deferred. It's a promise of a result that hasn't arrived yet. Instead of waiting for a database query to finish, you get a Deferred object and attach addCallback and addErrback to it.

def printResult(result):
    print "The data arrived:", result

def handleError(failure):
    print "Something went wrong:", failure

d = getRemoteData() # Returns a Deferred
d.addCallback(printResult)
d.addErrback(handleError)

Why Bother?

It feels complicated at first, but the benefit is massive. You can handle 10,000 concurrent connections on a single CPU because the process never sleeps. While one connection is waiting for a disk read, the reactor is already processing 50 other network packets.

Twisted is the future of Python networking. It's used by huge projects like Buildbot and even Apple's Calendar Server. If you're still using SocketServer and threading, you're living in the past.


Aunimeda builds production-grade backend systems - APIs, microservices, real-time applications, and system integrations.

Contact us for backend engineering services. See also: Custom Software Development, Web Development

Read Also

WebRTC: Scaling P2P Mesh Networking for Real-time Video (2019)aunimeda
Backend Engineering

WebRTC: Scaling P2P Mesh Networking for Real-time Video (2019)

Is it possible to build a video chat app without an expensive media server? In 2019, we're exploring the limits of WebRTC mesh networking.

eBPF: In-Kernel Programmability for Networking and Security (2018)aunimeda
Backend Engineering

eBPF: In-Kernel Programmability for Networking and Security (2018)

Linux 4.x is changing the game with eBPF. Why write kernel modules when you can run safe, verified bytecode directly in the kernel?

Node.js 0.2: Scaling to 10k Connections on One Threadaunimeda
Backend Engineering

Node.js 0.2: Scaling to 10k Connections on One Thread

Ryan Dahl's Node.js project has hit version 0.2. It's JavaScript on the server, but the real magic is the non-blocking I/O model.

Need IT development for your business?

We build websites, mobile apps and AI solutions. Free consultation.

Get Consultation All articles