The Twisted Matrix
Orbitz - orbitz - September 21, 2005It has been a long road from where I started to using Erlang. I certainly don’t plan on stopping with Erlang but so far I am quite the fan of it. On this road, which finally lead me here, I have used C, C++, Java, and Python, to name a few. I still have code for a multithreaded http I was writing in C hanging around.
I still use Python quite a bit, and one of the best tools in Python I have used is the Twisted Matrix framework. Prior to discovering Erlang (well more of introduced to it by noss, which i am deeply in debt for), I felt that Python was really *the* language for socket programming. Twisted is what made me think this.
Now, just to be clear, Erlang certainly does not make Python obsolete. Don’t confuse enthusiasm for Erlang with feeling every other language is useless. I introduced Python and Twisted into our development cycle at work and I am fairly pleased with the results.
For those that don’t know what Twisted is, it is a framework for developing software. What makes Twisted special is they have put a lot of work into making everything work in one thread. Making a server or client that handles multiple network connections is fairly trivial in Twisted.
So what makes Twisted work how it does? A uniform eventloop. In their case it’s called a reactor. There are a bunch of different kinds of reactors in Twisted. One needs to be made for whichever kind of event loop you are using. There is a select reactor (default), gtkreactor, gtk2reactor, wxwidgets reactor, and some sort of win32 reactor (I think still under development). The good thing is, most of your code can generally be written without caring about what reactor is being used. That is, atleast, the portions that don’t depend on gtk or win32 or wx.
Five years ago, when the Twisted project was first created, the authors feel that Python was the best language for the job. My question is, if Twisted was started today, would Python still be the best choice. I address part of the problem in a previous post. Part of the problem that I point out is, you have to do a lot of work to force everything into the event loop. That makes code reuse more difficult, and near impossible if you need to use a proprietary library that you can’t make non-blocking.
An obvious example of this is the adbapi module. adbapi module can use any DB-API 2.0 compliant module. This is to make adbapi useful. There are quite a few DB-API 2.0 compliant modules for Python so the best thing is to make use of them. adbapi’s API is not asynchronous or non-blocking though. Doing queries in the Twisted reactor thread means the entire application will stall until the query is finished. To solve this, Twisted does the queries in a thread. Twisted has to fall-back on the very thing it is trying to avoid in order to work. Python is particularly bad at threading too so Twisted tries to keep the number of threads to a reasonable amount. This means the number of queries you can have going concurrently is dependent on this.
A native implementation of the postgresql protocol has been implemented. It is called pgasync. I beleive the author has commented on how much quicker a large number of queries runs in his implementation, although I cannot find the quote. But has the same problem I pointed out before. Because Twisted is all in one thread, everything has to conform to its event loop making the implementation of the pgsql protocol useless. It had to be rewritten to work well in Twisted. This implementation is also fairly useless anywhere except Twisted. I cannot use this in an application that uses an event loop other than Twisted. Wouldn’t it be nice if I could use code between projects that arn’t dependent on Twisted? In a COL this is a non-issue. Simply run the database code in a process. Processes are part of the language so they are part of any program.
So, to get back to my question, would Python still be the choice of Twisted today, I don’t know. I asked the people of Twisted if they still would. For the most part, it seems like they didn’t quite know Erlang well enough to say if they would choose it. A few developers said one of the main reasons they chose Python was for its large standard library. In particular the ‘os’ module was pointed out as an important module that makes Python a good choice. However, after some research, most of the ‘os’ module is in Erlang’s standard library as well. In my opinion, I think the quality of the Erlang standard library and OTP is quite a bit better than Pythons. Python’s standard library seems to have suffered from a time period where anyone put anything they wanted into it. As a result, it is large, but a number of modules are fairly poor in quality.
Even if Python has a number of modules in its standard library that are nice for building Twisted, they still need to do a lot of work to force all of this code into the Twisted event loop. If you choose a COL, then you don’t have to do any work in forcing code into a single event loop. Instead, you have to do work to build the standard library that Python has. Is that a lot of work? Yes.
One could also argue, “There are a lot more Python projects than Erlang ones so there is a lot of code being written for Python that Twisted can make use of”. I think there is little contest in the point that there are more Python projects and programmers. But can Twisted really make use of this code? I think, in general, no, not without work. A lot of interesting things are probably going to involve some blocking somewhere which either needs to be pushed to a thread or rewritten not to block, such as the example I have already given.
The choice to use Python is a difficult one, I think. In my opinion, Python really does not offer much that Erlang doesn’t. I think, considering the services Twisted is trying to offer, a COL would be an excellent choice for it. Twisted does a lot of work trying to make asynchronous programming less confusing but writing concurrent programs is more natural. It feels more natural atleast.
I have had a lot of success using Twisted. I don’t think I’d write a socket program in Python without it. I also think I would generally not write a socket program in Python these days. Twisted and Erlang seem to be trying to accomplish much the same thing, although going about things in very different ways. In the end I think something like Erlang will win out. A COL seems to be saying, the world is concurrent so lets try to let people write in this natural way. Whereas, Twisted is forcing people to write in a rather artifical and unnatural way. Asynchronous programming works once you grasp it but even still the flow of the programs is rather awkward and hard to grasp I think.
I feel that, today, something like Twisted would be better off written in a COL. Let the work go into making powerful tools instead of massaging prewritten code into the event loop.
Categories: Blogs Orbitz
Comments
You informed me! This is an excellent article..
Posted by Dale E on 25 Sep 2009 at 19:08Türkiye’nin en büyük anne -bebek & aile yaşam platformu e-bebek ve Anadolu Ulaşım’ın işbirliğinde 1 Eylül 2009 tarihinde başlatılan, “çağrı merkezinden bilet alanlara çocuk koltuğu sağlanması”na yönelik uygulamaya olan ilgi artarak devam ediyor.
1 Haziran 2010 tarihinde yürürlüğe giren, ülkemizde oto koltuğu kullanımını zorunlu hale getiren yasa ile birlikte, seyahatlerde çocuk oto koltuğuna yönelik taleplerde ciddi artış yaşandığına dikkati çeken çocuk oto koltuğu Genel Müdürü Halil Erdoğmuş, ilerleyen dönemde bu talebin daha da artacağını öngördüklerinin altını çiziyor. astalavista :)
Posted by yeah on 23 Jun 2010 at 22:01While the actual aboriginal adaptation of askance was accounting in Java, and there are a few implementations of Deferreds and a simple reactor accounting in Java, they aren’t in a appear library state.
Unique and fabulous website. I visited many sites but this one is matchless. I am searching for the pass4sure 642-446 And about the pass4sure 642-426 and want to write an article about the pass4sure 642-873 which could not be ignored in this time frame.
I have started to learn Erlang ever since I read that some major medical companies have begun to utilize Erlang for their medical alert systems. I’m hoping knowing it will help increase my chances of landing a good job soon.
Posted by Ryan Y, on 03 Mar 2011 at 22:37It is a thrill ride twisted with a psychological thriller
Posted by cpsingh on 21 Mar 2011 at 21:21twisted transfer matrix of the six-vertex model at q being roots of unity. here the Free Templates are greate operatunity.
Posted by manish on 21 Mar 2011 at 21:26YouÂve got it in one. CouldnÂt have put it btteer.
Posted by Bardo on 05 May 2011 at 16:03I have heard that Erlang has been utilized in some new sla prototype services and machine interfaces. It seems that Erlang is on its way to becoming a must-know language for future programmers looking to get into the software side of the manufacturing industry. I came to this website to familiarize myself with it and think that I may attempt to delve deeper into the language.
Posted by Len630 on 03 Jun 2011 at 18:07Erlang has come a long way thanks to people like you keep up the awesome work
passive niche profits
Bill McRea I think that COL would be a good choice for twisted as well
Erlang can be quite useful, and sometimes a tampa car accident lawyer gets quite bogged down with legal paperwork. It would be interesting to see if the tech department could apply it to organizing legal documents.
Posted by AmyD on 15 Sep 2011 at 12:00It is a thrill ride twisted with a psychological thriller
ripenaxrau hjd electroluxap fgd tpersonalcar
Posted by dtyczka on 09 Nov 2011 at 07:24Developing software with Twisted is easy. Especially with my CPR training efforts. I’ll let you know how progress goes along.
Posted by Greg Smith on 23 Dec 2011 at 22:08de una cortina. Su voz es carnosa, como si fuera a escupir el pedazo de algo, pero ella no se inmutó ..!air conditioning repair los angeles
Posted by mivpl on 24 Jan 2012 at 17:24It is a thrill ride twisted…...................Medical Alert Systems
Posted by Medical Alert Systems on 30 Jan 2012 at 12:44This has been a great idea you have shared.
Posted by darrensy on 05 Feb 2012 at 01:56Fashion and style, children’s fashion, nike free run sale
Posted by nike free run sale on 24 Feb 2012 at 19:45I absolutely agree with the previous commenters. budapest
Posted by budapest on 13 Mar 2012 at 14:48Great blog, love the theme. It looks very neat and clean. jak szybko schudnąć
Posted by Robert D on 14 Mar 2012 at 23:43I am glad that you are liking it and I am so glad that at least you are now using the C, C++ and the other programming languages. I’ve been there too. Thanks, how to dispose of electrical appliances
Posted by Gema on 16 Mar 2012 at 19:59Thanks for some other wonderful article. The place else could anyone get that type of info in such an ideal method of writing? I have a presentation subsequent week, and I’m on the search for such information. PMP Traning Philadelphia | pmp
Posted by alex on 19 Mar 2012 at 18:43Add comment
Erlang on Twitter
» mogilnikov (Alexey Mogilnikov): RT @Burbass: I can now control my Mindstorm Lego car with Erlang. #erlang #mindstorm http://t.co/Jn78yViH
» rezasur (Reza Surya): Makasih ya ivansyah. Haaha wopp tidakk bisaa. RT @ivansyahhsn: Iya dewa erlang hbd,awas ya siksamu menanti
» bagus_erlang (bagus): Malu deh gw slh mencet ☹
» rezasur (Reza Surya): Makasih yaa tengku sharaaaRT @tengkushara: RT @fathiaamandaaa: RT @indrasan: selamat ulang tahun saudara reza erlang @rezasur semoga makin
» bagus_erlang (bagus): @bagus_erlang bangkok
» rezasur (Reza Surya): Maksih ya rianiRT @rianindahinyonk: RT @fathiaamandaaa: RT @indrasan: selamat ulang tahun saudara reza erlang @rezasur semog
» rezasur (Reza Surya): Makasih yoo bro aidilRT @aidilnasution: RT @fathiaamandaaa: RT @indrasan: selamat ulang tahun saudara reza erlang @rezasur sem
» rezasur (Reza Surya): Hehe makasi adek indyyy ☺RT @indytertuing: ƪ(^ヮ^)ʃ RT @fathiaamandaaa: RT @indrasan: selamat ulang tahun saudara reza erlang @rezasur
» hestipratiwi14 (hesti_rd): @bagus_erlang mentionss
» rezasur (Reza Surya): Haha makasi ya sodara ularrr ☺ RT @indrasan: selamat ulang tahun saudara reza erlang @rezasur semoga makin banyak proyek nya ya.
Statistics
Number of aggregated posts: 10503
Number of comments: 2137
Most recent article: May 21, 2012
Latest comments
» Erno on 100,000 Lines of Assembly Language: Excellent posting. Undoubtedly you are an expert when it comes to this writing. This is absolutely the first time I…
» Jessica on 30 September 2011: Basho Technologies, Erlang Solutions and Trifork AS Announce Big Data and NoSQL R: yeah of course. I just thought that everything will be just alright and I want to have these kind of…
» DRS786 on 25 May 2012: Poznan Erlang User Group Event: I’m going!