Friday, October 06, 2006

Twisted Python

This will not be a tutorial or explanation about Twisted. I plan to leave that for a later time. This is going to be a short reflection on why I love Twisted. Twisted Python aka "Twisted Matrix Laboratories", is an asynchronous networking framework that works inside of Python, and works well. When I was writing network code in C++, there was a lot of sockets, forking, etc. Most of the techniques are either synchronous, or require multiple threads, or both. Twisted works on one thread, and is non-blocking, so that once data is received, the thread sends data to a temporary thread, and returns to the listening loop in case more data is received. The documentation on the site is lacking, but once you wrap your skull around it, it makes sense. The most difficult thing for me was the absense of code. I had to write almost nothing to get a working server.

However, my favorite reason for getting into this OpenSource stuff, is the commenting. For example, in t.p.log, I ran across the following (from release for Python 2.4):

class EscapeFromTheMeaninglessConfinesOfCapital:
def own(self, owner):
warnings.warn("Foolish capitalist! Your opulent toilet will be your undoing!!",
DeprecationWarning, stacklevel=2)
def disown(self, owner):
warnings.warn("The proletariat is victorious.",
DeprecationWarning, stacklevel=2)

logOwner = EscapeFromTheMeaninglessConfinesOfCapital()

Well, it goes on. I'll leave you to find more of this humor. In the meantime, I'll be writing my windows service in twisted with named pipes so I can control it (meaning the client part of the server..heh) from a VC++ GUI. Also, my server is interfacing with Microsoft SQL Server (bleh), and I wrote a custom protocol for Twisted in a few hours...that will only work for my application. All of this stuff would have taken me a few days if not weeks in VC++, but now I only have to write the GUI, and not mess with ADO, wtih threading, with ANYTHING backend, which makes me happy...and it should you and your boss too.