Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Twisted an httplib2/socket replacement?

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.

like image 441
oparisy Avatar asked May 11 '09 06:05

oparisy


1 Answers

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."

like image 77
gimel Avatar answered Sep 24 '22 10:09

gimel