Parthenon Software Group


Networking in Python with Twisted

Posted by: Jessica Wilson 3 years, 8 months ago

There are all kinds of event-driven programming networks in existence today and each has strengths and weaknesses. With a set of powerful tools to handle Internet protocols, Twisted has emerged as one of the best for networking. Enabling writing, testing and code deployment, Twisted comes with application servers and protocol implementations for almost everything. Moreover, thanks to its foundation in Python, simple programming on a secure framework means quick turnaround on projects. Let's dig a little deeper into Twisted.


How does such a setup work

Twisted involves concurrent programming via an asynchronous server, an event loop and deferreds. You won't always have a value at the beginning of things, but you can expect a certain type of result based on what you do know. As a result, you can start with this expected result -the deferred. Deferreds act as placeholders for the actual process result and can receive and process a result that hasn't been computed yet.

At this point, the event loop comes into play via callback chains. There are multiple chains within each process and each one is supported by a deferred. Essentially what happens is that a value is passed through the chain or multiple chains to reach an end and thus complete an operation.

Where the asynchronous part come into play?

It's a matter of scheduling. Although not all values are/will be known, it's possible to arrange when an event occurs based on when the value reaches the event handlers at the end of the callback chain. This can also be arranged in advance so there isn't a complete backup while waiting for an event to occur. In that sense, multiple events can occur simultaneously.

So, what kinds of thing can Twisted be used for?

  • Client/server communication
  • Reservation systems
  • Chat services
  • System management and monitoring
  • and more

Moreover, Twisted has also been extended by various parties and such extensions have been made available to the public. You can find a list on The Twisted Community Code web page.

Confused? You’re not alone. For more information you can go to The Twisted Network Framework or TwistedMatrix.com.