views:

609

answers:

2

Many python libraries, even recently written ones, use httplib2 or the socket interface to perform networking tasks.

Those are obviously easier to code on than Twisted due to their blocking nature, but I think this is a drawback when integrating them with other code, especially GUI one. If you want scalability, concurrency or GUI integration while avoiding multithreading, Twisted is then a natural choice.

So I would be interested in opinions in those matters:

  1. Should new networking code (with the exception of small command line tools) be written with Twisted?
  2. Would you mix Twisted, http2lib or socket code in the same project?
  3. Is Twisted pythonic for most libraries (it is more complex than alternatives, introduce a dependency to a non-standard package...)?

Edit: please let me phrase this in another way. Do you feel writing new library code with Twisted may add a barrier to its adoption? Twisted has obvious benefits (especially portability and scalability as stated by gimel), but the fact that it is not a core python library may be considered by some as a drawback.

+5  A: 

See asychronous-programming-in-python-twisted, you'll have to decide if depending on a non-standard (external) library fits your needs. Note the answer by @Glyph, he is the founder of the Twisted project, and can authoritatively answer any Twisted related question.

At the core of libraries like Twisted, the function in the main loop is not sleep, but an operating system call like select() or poll(), as exposed by a module like the Python select module. I say "like" select, because this is an API that varies a lot between platforms, and almost every GUI toolkit has its own version. Twisted currently provides an abstract interface to 14 different variations on this theme. The common thing that such an API provides is provide a way to say "Here are a list of events that I'm waiting for. Go to sleep until one of them happens, then wake up and tell me which one of them it was."

gimel
Your answer is detailed but I already know (some of) Twisted. What I am interested here is how other developpers may perceive it, and if some may choose not to use my library because it does depend on a non-core library. You know, in the same way that some of them will not integrate GPL code in their source for license compatibility reasons.
Tanelorn
Additional dependencies should always be considered with care. I my limited experience, Twisted is a little slow in catching up with Python versions, but quite solid.
gimel
A: 
  1. Should new networking code (with the exception of small command line tools) be written with Twisted?
    • Maybe. It really depends. Sometimes its just easy enough to wrap the blocking calls in their own thread. Twisted is good for large scale network code.
  2. Would you mix Twisted, http2lib or socket code in the same project?
    • Sure. But just remember that Twisted is single threaded, and that any blocking call in Twisted will block the entire engine.
  3. Is Twisted pythonic for most libraries (it is more complex than alternatives, introduce a dependency to a non-standard package...)?
    • There are many Twisted zealots that will say it belongs in the Python standard library. But many people can implement decent networking code with asyncore/asynchat.
Unknown
-1. Point 1: twisted is great for any scale of network code, your restriction doesn't apply. Threaded-wrapped code has worst performance and is hard to get right and to debug. Point 2: No. If I have the full twisted stack on my disposal why would I use http2lib or socket? Pont 3: Asyncore/asynchat, seriously? :(
nosklo
@nosklo: Point 1: you are the kind of Twisted Zealot I am talking about. There is no arguing that twisted is more complex. Thread-wrapped code is easy to get right if you use Queue for interthread communication, I highly doubt the performance will matter for most applications. Point 2: Are you thinking of making a web server? I think not. Point 3: Asyncore/Asynchat seriously... See point 1.
Unknown
Guys, YMMV, especially depending on your experience with a toolkit or the other. I am afraid my original question was too subjective.I am not so much concerned by the perceived difficulty of Twisted (it is my problem to learn it, and I think it is documented enought so that possible maintainers of this code will not be lost) than by the fact that it may limit its acceptability to other coders.
Tanelorn
@Unknown: Point1: For every piece of async code that you can write directly with sockets, I can write the same in a simpler, more readable way by using twisted. Specially for small scale. Point2: I am talking about *mixing twisted and httplib/socket code* not making web servers. I don't think there's a point, since if I have twisted why would I use sockets/httplib directly?? 3) asyncore/asynchat sucks, and you know it.
nosklo
I accepted your answer due to the worthy debate in comments :)
Tanelorn
@nosklo: sorry but it is not easier to start up the twisted engine if all you need to do is urllib2.urlopen("website.com"). And yes asyncore and asynchat is not as complete, but its also not as crufty. If you have ever run Twisted in windows you will notice that it opens up a loopback port. Why does it need to do that when async/ore/chat doesn't? Also: Twisted = 6 mb Asycore/chat = 32kb
Unknown