What's so cool about Twisted?

Question:

I’m increasingly hearing that Python’s Twisted framework rocks and other frameworks pale in comparison.

Can anybody shed some light on this and possibly compare Twisted with other network programming frameworks.

Asked By: Anton Gogolev

||

Answers:

Well it’s probably according to taste.

Twisted allows you to easily create event driven network servers/clients, without really worrying about everything that goes into accomplishing this. And thanks to the MIT License, Twisted can be used almost anywhere. But I haven’t done any benchmarking so I have no idea how it scales, but I’m guessing quite good.

Another plus would be the Twisted Projects, with which you can quickly see how to implement most of the server/services that you would want to.

Twisted also has some great documentation, when I started with it a couple of weeks ago I was able to quickly get a working prototype.

Quite new to the python scene please correct me if i’m in the wrong.

Answered By: Johann du Toit

There are a lot of different aspects of Twisted that you might find cool.

Twisted includes lots and lots of protocol implementations, meaning that more likely than not there will be an API you can use to talk to some remote system (either client or server in most cases) – be it HTTP, FTP, SMTP, POP3, IMAP4, DNS, IRC, MSN, OSCAR, XMPP/Jabber, telnet, SSH, SSL, NNTP, or one of the really obscure protocols like Finger, or ident, or one of the lower level protocol-building-protocols like DJB’s netstrings, simple line-oriented protocols, or even one of Twisted’s custom protocols like Perspective Broker (PB) or Asynchronous Messaging Protocol (AMP).

Another cool thing about Twisted is that on top of these low-level protocol implementations, you’ll often find an abstraction that’s somewhat easier to use. For example, when writing an HTTP server, Twisted Web provides a “Resource” abstraction which lets you construct URL hierarchies out of Python objects to define how requests will be responded to.

All of this is tied together with cooperating APIs, mainly due to the fact that none of this functionality is implemented by blocking on the network, so you don’t need to start a thread for every operation you want to do. This contributes to the scalability that people often attribute to Twisted (although it is the kind of scalability that only involves a single computer, not the kind of scalability that lets your application grow to use a whole cluster of hosts) because Twisted can handle thousands of connections in a single thread, which tends to work better than having thousands of threads, each for a single connection.

Avoiding threading is also beneficial for testing and debugging (and hence reliability in general). Since there is no pre-emptive context switching in a typical Twisted-based program, you generally don’t need to worry about locking. Race conditions that depend on the order of different network events happening can easily be unit tested by simulating those network events (whereas simulating a context switch isn’t a feature provided by most (any?) threading libraries).

Twisted is also really, really concerned with quality. So you’ll rarely find regressions in a Twisted release, and most of the APIs just work, even if you aren’t using them in the common way (because we try to test all the ways you might use them, not just the common way). This is particularly true for all of the code added to Twisted (or modified) in the last 3 or 4 years, since 100% line coverage has been a minimum testing requirement since then.

Another often overlooked strength of Twisted is its ten years of figuring out different platform quirks. There are lots of undocumented socket errors on different platforms and it’s really hard to learn that they even exist, let alone handle them. Twisted has gradually covered more and more of these, and it’s pretty good about it at this point. Younger projects don’t have this experience, so they miss obscure failure modes that will probably only happen to users of any project you release, not to you.

All that say, what I find coolest about Twisted is that it’s a pretty boring library that lets me ignore a lot of really boring problems and just focus on the interesting and fun things. 🙂

Answered By: Jean-Paul Calderone
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.