Untwisting Twisted!
UltimateCoder (~ultimatecoder) |
Description:
Abstract
Twisted is a framework for writing event-driven applications in Python. Event driven is a well-recommended programming paradigm for writing high performance networking applications, GUI applications or device drivers. The framework is more than a decade old and has a large community base. I am proposing an entry-level workshop to share my expertise with the community.
Description
Recently I was employed to write a device driver for an application based on an ARM platform. I was impressed with the power of Twisted for writing a network intensive application. It was a bumpy ride to learn Twisted.
Twisted is an old giant. There are plenty of resources available to get started with this framework. In my knowledge, most of them are focusing on the applicability of the framework thus they are lacking in explaining the core of the framework. I have tried my best to craft this workshop for beginners who are only expected to have knowledge of Python. Most components will end with hands-on examples.
Below is the a rough outline of topics I have planned to conduct as part of this workshop.
Outline
- Introduction
- What is an event-driven programming paradigm?
- Eventloop
- Installation
- Windows
- GNU/Linux
- Macintosh
- Important components of the framework
- Reactor
- Transports
- Protocol
- Protocol factories
- Callbacks
- Deferreds
- Testing
- Demonstration of writing tests for a real-world application
- Guidelines on writing tests
- Deployment guidelines
Seeking help from the community
- Mailing list
- IRC
- Forum
- Blog
Introduction
I will explain the gist of event-driven programming paradigm. I will do a small comparison of this with the existing paradigm. Event-loops are the core of the event driven programming. I will explain how the loop communicates for scheduling tasks.
Time: 20 minutes
Installation
Twisted is largely platform independent, but also includes platform-specific functionalities. Installation of Twisted is as simple as installing any Python package.
Time: 10 minutes
Components of the Framework
Reactor
The reactor is an event-loop which drives the application.
Time: 20 minutes
Callbacks
Callbacks are a fundamental part of event-driven programming and are the way that the reactor indicates to an application that an event has arrived. There is two type of callbacks. One is when the event has succeeded and the another is when the event has failed. It is quite common to tangle the logic between success and failure callbacks. Twisted provides a great solution to write these callbacks by giving deferred interfaces.
Time: 20 minutes
Transport
Transport is a collection of various transport classes. It provides an API to transfer bytes to another end of the socket.
Time: 20 minutes
Protocol
The protocol describes how to react to events. Protocol factory is responsible for producing an instance of the protocol. All the configuration information are preserved in protocol factories.
Time: 15 minutes
Testing
Testing Twisted code is tricky. I will demonstrate mocking response objects and show how to avoid polluting an event loop. I will be focusing on utilities of "twisted.trial" module.
Time: 30 minutes
Deployment guidelines
Deploying a Twisted-based application is like deploying any other Python application. I will point out various ways to deploy and maintain a Twisted-based application.
Time: 10 minutes
Seeking help from the community
I will conclude this workshop by sharing resources to get involved with the community and continue to learn.
Time: 5 minutes
Detailed content
Duration
150 minutes
Presented At
Built-in Async IO vs Twisted
Both Async IO and Twisted are fundamentally solving a problem of supporting event driven asynchronous code in Python. However, Async IO is just a collection of higher level functions which can be used to write an event driven code. Whiles Twisted is having a huge collection of higher level ready to go classes. These classes are more goal oriented which you can inherit and customize your requirements. These classes save a lot of time of developers. For example if my goal is to write a high performance IRC client then I have to write lot of custom logic if I use Async IO, but Twisted has built-in higher level classes for both IRC client and server. It is possible to write working IRC bot using Twisted in less than 10 minutes. And the Twisted framework is not limited with IRC classes, but it has classes for writing SSH, Telnet, SSL, TCP, UDP, Email (SMTP, POP3 client and server, IMAP), DNS, HTTP, Raw TCP/UDP sockets and many more.
Twisted was invented 16 years ago. At that time there was no built-in support for writing asynchronous code in Python. Since async io has migrated as a built-in module, lot of Twisted API is internally migrating to Async IO code.
Prerequisites:
One workstation with Python3 installed
Content URLs:
Speaker Info:
A full-stack developer by profession, a computer scientist by heart and natural born actor. One of the core organizer of https://pykachchh.github.io/workshop/PyKutch 2016.