Hacker News new | ask | show | jobs
by vosper 4926 days ago
Twisted is the least accessible of the big Python libraries that I've come across. I've come to it a couple of times when it seemed like it would suit a problem I had, only to throw up my hands in frustration and go look for a solution somewhere else.

Unfortunately this article did nothing to enlighten me - it has some pretensions of being philosophical without explaining any of the practicalities.

Does anyone use Twisted in production? Do you consider it worth the learning curve, and would you use it again if you were implementing your system now?

3 comments

I've used it, and had some success with it, but now use my own stack for network programming.

I struggled with twisted several times without getting it. Then I did some non-blocking socket/select programming and thought, "I wish I had a library that.." and then realised what twisted did.

There's a few reasons I'm not using it now. Main one is that I've turned against frameworks. I find it's generally easier to understand a problem and write the code than to really learn a generic framework and coax it to your needs. And by just writing the code yourself you don't have to deal with dependencies, you have more freedom to make changes, debugging is easier, it's easy to deploy in a restricted environment.

Also, I have a rule of thumb when I'm writing python that if I'm using inheritance I'm doing something wrong. I have huge trouble understanding duck-type code that uses inheritance. Encapsulation is always possible, and I find it a lot easier to refactor. Twisted uses inheritance extensively. The docs encourage you to extend their classes. In places the application developer is expected to reach into framework objects and poke variables.

Your restricted environments don't allow virtualenvs?

It's interesting that inheritance vs composition comes up, since one of the most common criticisms I've heard is actually the opposite (that there's too many different objects to get things done).

However, I do understand why you might think this: protocols, which is probably the first thing you touch in Twisted, are usually implemented through inheritance. For resources, the default implementation that you subclass (twisted.web.resource.Resource) contains a bunch of behavior that you almost certainly want.

The important thing to remember is that this inheritance is only to get default behavior. If you want to implement it without inheritance, Twisted very clearly and explicitly defines the interface you have to implement:

https://twistedmatrix.com/documents/current/api/twisted.inte...

https://twistedmatrix.com/documents/current/api/twisted.web....

These are just two examples: Interfaces are all over twisted.

And, to quote from the article:

  It doesn't need to be that way, though.  When you create
  an object, it is best to create it as independently as
  possible; to test it in isolation; to discretely separate
  its every interaction with the outside world so that they
  may be carefully controlled, monitored, intercepted an
  inspected, one iteration at a time.
This has recently come up in the async-pep conversation on Python-ideas. If you have a significantly better solution for e.g LineReceiver, I'd love to steal it!
Thanks for feedback. #twisted ppl were very helpful to me in the early days of me learning about network programming and python.

I'm impressed with the achievement of Twisted. For example, the way it offers support for both unix-way async (select, getabyte getabyte), and the Windows/AIX alternative where you can make a system call that says "get back to me when you have a complete message that I can consume in one hit". It'll be way faster and more polished than the stuff I write too.

Part of the reason there's so many objects in it is because it does so damn much - there's support for all sorts of existing protocols in twisted. And - a powerful framework can't entirely abstract away the layering. If it does, it stops being powerful.

It might be possible to use virutalenvs. I keep meaning to look into these. I am skeptical of frameworks though. You become vulnerable to version changes, you can't take knowledge to a new platform.

Very interested to see something from the python-ideas conversation in the standard library. I'll have a serious go at whatever they end up with, and wouldn't mind if it was a stripped down twisted but with composition rather than inheritance. It may be the feature that pushes python people to v3. I'll look over the links you sent regarding this.

I use Twisted and love it. What really helped me was reading the source code instead of trying to decipher the documentation. The code is really readable and things started clicking once I made that investment.
If you don't mind - what did you build with it? Was Twisted the only suitable library - what about Tornado?

I'm curious because Twisted has been around for ages and seems to be in active development but I've never talked to anyone who actually uses it.

It is my understanding that Spotify is (or at least was, when I saw their presentation), pretty much a Giant Twisted App with some C++ for parts that were too slow.

Lucasfilm, justin.tv, Launchpad and TweetDeck are some of the larger names you may have heard of. A bunch of smaller startups do too.

If you've been doing XMPP you've probably used a Twisted server (it's either that or ejabberd, mostly) at some point.

If you want Tornado, why not use Cyclone? It's a port (to Twisted) of Tornado; you get to write webby parts just like tornado's and you get the benefit of all of Twisted when you need it.

FWIW, I did the Twisted Help Desk and co-hosted the Twisted tutorial two years in a row now at EuroPython, and two years in a row that tutorial has had to move to a bigger room. Asking around showed a large portion of people were there because there place of work was either using or considering using Twisted. I never cease to be surprised by the stories I hear of people using Twisted to great success for pretty nifty applications.

I've used twisted in a quite a few production projects. One of them was for the management interface of a wireless switch. The best part about twisted is that it comes with about two dozen client and server implementations for various standard protocols. At another one of my previous gigs, we were using Cyclone (the twisted port of Tornado) for a document management system. Eventually, the customers wanted the ability to upload documents via (S)FTP using the same credentials their users used for the web based system. With I was able to code something up in about an hours time. I believe it wouldn't have been that easy if we weren't using twisted.
Unfortunately, I think my employer's NDA prevents me from talking about most of what I've developed. I will say that one of the projects I used combined IRC, SMTP, and HTTP together in one server. Twisted made that incredibly easy to write, and be performant. AFAIK, Tornado is HTTP only at this point.
> What really helped me was reading the source code instead of trying to decipher the documentation.

That's an awful excuse. People rely on libraries in order to do their jobs. A "worse" library with better documentation helps more people.

Yes, I use Twisted in production. I absolutely consider it worth the learning curve. And I've started new projects in it in the last 3 weeks, so I guess that answers your final question.