Nabaztag is an Armenian word for “rabbit” and is also the name of an unusual internet-connected device. (See http://nabaztag.com.)
This little rabbit is one of the “internet of things” developed by a company named Violet. Nabaztag can respond to HTTP methods to move his ears, speak a sentence, play a sound or song, or flash some lights where his mouth should be. Nabaztag can sense objects that have attached RFID tags. In a potential application a user could wave his keys in front of the rabbit as he prepared to leave the house. Nabaztag would arrange to turn off his home lights and open the garage door. Other people have used the rabbits to read out stock quotes.
To use the rabbits, one registers the serial number of the particular rabbit with a central server that routes HTTP methods to the appropriate device. Users have reported frustrations with the quality of this service. I personally can’t imagine casual users up to the task of registering this device with their local wireless network and then registering the device with the server, but the device opens up an intriguing look into the possible “internet of things.”
TwistedMatrix (http://twistedmatrix.com) is an asynchronous framework for building networked applications. When writing networked applications that open a many ports to other servers and processes, one can end up with a slew of threads and a confusing mess of locks, queues and semaphores. Asynchronous frameworks eliminate synchronization problems by presenting the user with a single thread of control.
The twisted matrix core “reactor” manages all sockets and OS events. You can think of it as a giant “select” loop that dispatches back to your code. When your code is running, it is the only thread, so there is no need to use mutexes and semaphores to protect access to your data structures.
Integrating windowing environments with asynchronous network programming presents a challenge. In a threaded approach, the window environment is usually given its own thread to maintain user responsiveness. Networking and other activities are usually implemented in other threads. Managing windows in an asynchronous framework can be complicated, but TwistedMatrix provides standard “reactors” for popular window toolkits like GTK. With it, the reactor dispatches back to user code for window events, network events and timer events all in a similar fashion. Very elegant!
I explored using Twisted for a small sensor application with an attached web server. My application needed a serial port interface, a worker thread, and a webserver. The Twisted asynchronous approach allowed me to develop each of these in isolation without much concern for concurrency issues.
As an experiment, I ported this small application to a raw Python using threads, locks and queues to see how it compared. The code got slightly larger, and a little more convoluted with the addition of the necessary concurrency locks. But being a small application, with only three threads, it was manageable. Having compared the two approaches I think that if I needed to scale up the application to support hundreds or thousands of connections, I would choose the asynchronous approach and give Twisted the job.