SlideShare a Scribd company logo
WTF is Twisted?
(or: Owl Amongst The Ponies)
About HawkOwl
• hawkowl@atleastfornow.net
• @hawkieowl on Twitter
• hawkowl on GitHub, Keybase &
ThoughtStreams
• PGP Key Fingerprint:

2100 5027 897E C8AE 1D94

A905 2308 B479 D392 4A11
• Twisted Core Committer and Release
Manager (13.2 & 14.0)
• Is not actually an owl
Twisted in One Slide
• Twisted is a framework for writing network
applications in Python
• Best known for “Deferreds”, Twisted’s async
abstraction
• Contains protocol implementations for HTTP,
SMTP, IMAP, DNS, etc.
Why would you want to use
Twisted?
• Higher performance in some common situations
• Stable, mature codebase
• Protocol implementations for lots of things
• High test coverage
• Great community consisting of excellent
individuals who do excellent work
What makes Twisted especially
different from other projects?
• Twisted has a reputation for being the “black
sheep” in the Python community, mostly
because we do things “weirdly” and “hard”
• But, surprise! It turns out the Twisted developers
were right all along (see: asyncio)
• Different set of principals, from both the
longevity of the project and their specific
problems
The Tenets of Twisted
(the time-tested, battle-hardened principals of the
Twisted project, as interpreted by HawkOwl)
Subclassing is bad.
Composition is the new inheritance!
Multiple inheritance is
really bad.
Handy hint: If you’ve written the word “super”, it’s
already too late.
Time-based releases
are king.
For large, evolving frameworks, it’s better to gradually
change than have a massive app migration effort.
Early APIs > No APIS
You won’t know how your API works in the real world until
other people use it. You can always deprecate it later.
All code needs tests.
No exceptions.
It doesn’t matter if you’re fixing an urgent release regression
- an eye on coverage saves you a lot of time later.
All code needs reviewing.
No exceptions.
Code review is invaluable, even if it does descend into
bikeshedding if they can’t find anything wrong.
Only bad APIs can’t
be unit tested.
Can’t unit test your API? Too bad, time to refactor it.
Build slaves are
amazing.
Test your code on everything. Virtual machines are
cheap, compatibility problems down the line are not.
–David Reid, Glyph, et al
“Call a function with some arguments and
inspect some return values.”
How Twisted Works
(a short, mostly-correct overview)
The Event Loop
(Spin me right round, baby, right round)
The Reactor
• Twisted, being an event-driven framework,
provides an event loop, or “reactor”.
• This is an infinite loop, wrapped around a blocking
call that waits until some I/O is ready
• Once the I/O is ready, this fires off an event to run
the code that was waiting for it
• Runs other tasks while I/O blocked tasks are
waiting
Using the Reactor
• To utilise the reactor, you use Deferreds.
• Deferreds are an “IOU” for an actual result
sometime in the future
• A Deferred contains a callback chain, a series of
functions run in sequence after it gets a result
• Deferreds get their results from events occurring -
such as I/O being received or some task being
finished
Benefits
• For I/O bound applications, allows you to
efficiently use CPU resources, as the reactor
runs other things in the meantime
• Integration with common event loops - for
example, GTK or QT, to use your Twisted code
into your GTK or QT applications
The Callback Chain
(Hey, I just met you, and this is crazy,

but here’s a function, so call it maybe?)
The Callback Chain
• The easiest way to understand the callback
chain is to visualise it as a production line.
• A production line is made up of discrete blocks -
each doing a separate thing.
• Let’s imagine a such a production line…
Lets say that we are building a toy car, and we
need to do the following:
1. Create toy car body from materials
2. Paint racing stripes on car body
3. Add wheels to the car body
4. Put the completed car in a box
Here is how we would do that in psuedo-code
Deferreds:
!
! d = Deferred()!
!d.addCallback(buildCarBody)!
d.addCallback(paintStripes)!
d.addCallback(addWheels)!
d.addCallback(placeInBox)!
# Start the callback chain!
d.callback(rawMaterials)
Deferreds
• Each function in the chain is called with the
result of the previous one as its first argument
• Functions in the callback chain can return either
a result or a Deferred that fires with a result
sometime in the future
Lists of Deferreds
• If you have several Deferred operations to do at
the same time, and want your callback to fire
only when they are all done, use
DeferredList.!
• Pass it a list of Deferreds, and it will return a
Deferred that fires with a list containing the
results of each when they have all finished.
Error Handling
• Error handling is done by errbacks.
• Errbacks are like callbacks, but are run when an
exception is raised.
• An errback will catch all errors that occur on the
chain before it.
Deferred Benefits
• Forces you to break up your logic into sections,
broken up over external interface accesses
• Encourages reuse, as these sections quite
commonly do one thing to an input and return it
• Can be cancelled (eg. if a client disconnected
and you don’t care about the result anymore)
Deferred Benefits
• Discourages global mutable state by having
different execution threads share the same
global state
• No global mutable state means you can scale
easier
• If your global state is databases, just spin up
more Twisted instances to use all your CPUs
Extending the Callback Chain
(Because sometimes things like to do other things)
Deferreds returning
Deferreds
• Deferreds can contain their own callback chain -
meaning that one Deferred returned may have
several callbacks being called inside it
• This is invisible to the “top level” of callbacks
• You can extend your callback chain at any time
What can Twisted do?
(Enough of the boring stuff!)
Web
• Klein, a Flask-like API in Twisted
• Saratoga, a HTTP API framework
• Autobahn, a Websockets implementation
• Hendrix, a Django deployment framework
• Static file serving: 

twistd -n web --port tcp:8080 --path /tmp
Klein (Flask in Twisted)
from klein import Klein!
app = Klein()!
!
@app.route('/')!
def home(request):!
return 'Hello, world!'!
!
app.run("localhost", 8080)
Saratoga (HTTP API)
{!
"metadata": {!
"name": “APIExample", "versions": [1]!
},!
"endpoints": [{!
! ! "endpoint": “example", "getProcessors": [!
{"versions": [1]}!
]}!
]}!
from saratoga.api import SaratogaAPI!
import json!
!
class APIExample(object):!
class v1(object):!
def example_GET(self, request, params):!
return {"hello": "world"}!
!
api = SaratogaAPI(APIExample, json.load(open("apidef.json")))!
api.run()
Email
• Complete SMTP and ESMTP server and clients
• POP3 and IMAP4 servers and clients
• Support for TLS-secured email out of the box



For more info, see:

http://twistedmatrix.com/documents/
current/mail/index.html
DNS
• A caching, recursive DNS server out of the box

twistd -n dns -c —port 10053 --recursive
• DNS client APIs
• DNS server toolkit



For more info, see:

http://twistedmatrix.com/documents/
current/names/howto/custom-server.html
Chat
• IRC Server

twistd -n words --irc-port tcp:8400
--group roomname --auth memory:usr:pass
• IRC client (for making IRC bots)
• XMPP client



For more info, see: 

http://twistedmatrix.com/documents/
current/words/examples/
Want to know more?
• Twisted Docs

http://twistedmatrix.com/documents/current/
• Iffy’s Twisted FTW

http://iffycan.com/twistedftw/
• HawkOwl’s Technicals

http://technicals.atleastfornow.net/
Questions?
(sane answers not guaranteed)

More Related Content

PPTX
Asynchronous Python with Twisted
PDF
An Introduction to Twisted
KEY
Twisted: a quick introduction
ZIP
How we use Twisted in Launchpad
PDF
Twisted Introduction
PDF
Snabb Switch: Riding the HPC wave to simpler, better network appliances (FOSD...
PDF
Netty @Apple: Large Scale Deployment/Connectivity
PPTX
DCUS17 : Docker networking deep dive
Asynchronous Python with Twisted
An Introduction to Twisted
Twisted: a quick introduction
How we use Twisted in Launchpad
Twisted Introduction
Snabb Switch: Riding the HPC wave to simpler, better network appliances (FOSD...
Netty @Apple: Large Scale Deployment/Connectivity
DCUS17 : Docker networking deep dive

What's hot (20)

PPT
Netty 4-based RPC System Development
PDF
Securing & Enforcing Network Policy and Encryption with Weave Net
PDF
iptables and Kubernetes
ODP
Developing high-performance network servers in Lisp
PPTX
Evented Ruby VS Node.js
PDF
Building scalable network applications with Netty (as presented on NLJUG JFal...
PDF
IP Virtual Server(IPVS) 101
PPTX
Kubernetes @ Squarespace (SRE Portland Meetup October 2017)
PDF
Microservices with Micronaut
PDF
Loom and concurrency latest
ODP
Building Netty Servers
PDF
[En] IPVS for Docker Containers
PDF
Docker and Fluentd
PDF
From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016
PPT
Reactive programming with examples
PDF
[네이버오픈소스세미나] What’s new in Zipkin - Adrian Cole
PDF
Mininet: Moving Forward
PDF
Head First to Container&Kubernetes
PDF
iptables 101- bottom-up
KEY
Event machine
Netty 4-based RPC System Development
Securing & Enforcing Network Policy and Encryption with Weave Net
iptables and Kubernetes
Developing high-performance network servers in Lisp
Evented Ruby VS Node.js
Building scalable network applications with Netty (as presented on NLJUG JFal...
IP Virtual Server(IPVS) 101
Kubernetes @ Squarespace (SRE Portland Meetup October 2017)
Microservices with Micronaut
Loom and concurrency latest
Building Netty Servers
[En] IPVS for Docker Containers
Docker and Fluentd
From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016
Reactive programming with examples
[네이버오픈소스세미나] What’s new in Zipkin - Adrian Cole
Mininet: Moving Forward
Head First to Container&Kubernetes
iptables 101- bottom-up
Event machine
Ad

Viewers also liked (20)

PDF
Building an inflight entertainment system controller in twisted
PPT
OSCon - Performance vs Scalability
PDF
Обзор фреймворка Twisted
PDF
Python twisted
PDF
আহত সাপ
PDF
Las 5 preocupaciones en Twitter sobre la salida de Claudio Palma en Twitter
PDF
Oto cycles 1
DOCX
47. EPISTOLA 2CORINTENI ORIGINAL ANTIC -FACSIMIL
PDF
Proyecto daphne es
PPT
SBRN: How to use Facebook For Business
PPTX
Legado de un Paciente Critico Terminal UP Med
PPTX
Cuadro sinóptico
PPT
Instrumentos
PPTX
Presentación holacracia
PPTX
Cartas a julieta
PDF
China eCommerce Market Analysis Report 2013 – Chapter 2: Characteristics and ...
PDF
Leading HR Practices
PPTX
Bombas centrífugas
Building an inflight entertainment system controller in twisted
OSCon - Performance vs Scalability
Обзор фреймворка Twisted
Python twisted
আহত সাপ
Las 5 preocupaciones en Twitter sobre la salida de Claudio Palma en Twitter
Oto cycles 1
47. EPISTOLA 2CORINTENI ORIGINAL ANTIC -FACSIMIL
Proyecto daphne es
SBRN: How to use Facebook For Business
Legado de un Paciente Critico Terminal UP Med
Cuadro sinóptico
Instrumentos
Presentación holacracia
Cartas a julieta
China eCommerce Market Analysis Report 2013 – Chapter 2: Characteristics and ...
Leading HR Practices
Bombas centrífugas
Ad

Similar to WTF is Twisted? (20)

PDF
Asynchronous I/O in Python 3
PDF
Twisted
PPTX
The Onward Journey: Porting Twisted to Python 3
PDF
Building Web APIs that Scale
PPTX
Async programming and python
PDF
Twisted logic
PPTX
Using Coroutines to Create Efficient, High-Concurrency Web Applications
PDF
Python, do you even async?
PDF
Scaling Django with gevent
PDF
Async - react, don't wait - PingConf
PDF
Async Web Frameworks in Python
KEY
Node.js Presentation Rotterdam.PHP
PDF
Paul Campbell — A Modern Approach to Third-Party Embedded Widgets (Turing Fes...
PPTX
4Developers 2015: Programowanie synchroniczne i asynchroniczne - dwa światy k...
PDF
The future of async i/o in Python
PDF
Twisted presentation & Jamendo usecases
PPTX
Introducing to Asynchronous Programming
PDF
Matthew Eernisse, NodeJs, .toster {webdev}
PDF
Awesome Concurrency with Elixir Tasks
PDF
Tornado in Depth
Asynchronous I/O in Python 3
Twisted
The Onward Journey: Porting Twisted to Python 3
Building Web APIs that Scale
Async programming and python
Twisted logic
Using Coroutines to Create Efficient, High-Concurrency Web Applications
Python, do you even async?
Scaling Django with gevent
Async - react, don't wait - PingConf
Async Web Frameworks in Python
Node.js Presentation Rotterdam.PHP
Paul Campbell — A Modern Approach to Third-Party Embedded Widgets (Turing Fes...
4Developers 2015: Programowanie synchroniczne i asynchroniczne - dwa światy k...
The future of async i/o in Python
Twisted presentation & Jamendo usecases
Introducing to Asynchronous Programming
Matthew Eernisse, NodeJs, .toster {webdev}
Awesome Concurrency with Elixir Tasks
Tornado in Depth

Recently uploaded (20)

PDF
System and Network Administration Chapter 2
PDF
System and Network Administraation Chapter 3
PPTX
L1 - Introduction to python Backend.pptx
PDF
medical staffing services at VALiNTRY
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Nekopoi APK 2025 free lastest update
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
Transform Your Business with a Software ERP System
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Design an Analysis of Algorithms II-SECS-1021-03
System and Network Administration Chapter 2
System and Network Administraation Chapter 3
L1 - Introduction to python Backend.pptx
medical staffing services at VALiNTRY
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Adobe Illustrator 28.6 Crack My Vision of Vector Design
CHAPTER 2 - PM Management and IT Context
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Nekopoi APK 2025 free lastest update
Online Work Permit System for Fast Permit Processing
Transform Your Business with a Software ERP System
Operating system designcfffgfgggggggvggggggggg
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Odoo Companies in India – Driving Business Transformation.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
Design an Analysis of Algorithms II-SECS-1021-03

WTF is Twisted?

  • 1. WTF is Twisted? (or: Owl Amongst The Ponies)
  • 2. About HawkOwl • hawkowl@atleastfornow.net • @hawkieowl on Twitter • hawkowl on GitHub, Keybase & ThoughtStreams • PGP Key Fingerprint:
 2100 5027 897E C8AE 1D94
 A905 2308 B479 D392 4A11 • Twisted Core Committer and Release Manager (13.2 & 14.0) • Is not actually an owl
  • 3. Twisted in One Slide • Twisted is a framework for writing network applications in Python • Best known for “Deferreds”, Twisted’s async abstraction • Contains protocol implementations for HTTP, SMTP, IMAP, DNS, etc.
  • 4. Why would you want to use Twisted? • Higher performance in some common situations • Stable, mature codebase • Protocol implementations for lots of things • High test coverage • Great community consisting of excellent individuals who do excellent work
  • 5. What makes Twisted especially different from other projects? • Twisted has a reputation for being the “black sheep” in the Python community, mostly because we do things “weirdly” and “hard” • But, surprise! It turns out the Twisted developers were right all along (see: asyncio) • Different set of principals, from both the longevity of the project and their specific problems
  • 6. The Tenets of Twisted (the time-tested, battle-hardened principals of the Twisted project, as interpreted by HawkOwl)
  • 7. Subclassing is bad. Composition is the new inheritance!
  • 8. Multiple inheritance is really bad. Handy hint: If you’ve written the word “super”, it’s already too late.
  • 9. Time-based releases are king. For large, evolving frameworks, it’s better to gradually change than have a massive app migration effort.
  • 10. Early APIs > No APIS You won’t know how your API works in the real world until other people use it. You can always deprecate it later.
  • 11. All code needs tests. No exceptions. It doesn’t matter if you’re fixing an urgent release regression - an eye on coverage saves you a lot of time later.
  • 12. All code needs reviewing. No exceptions. Code review is invaluable, even if it does descend into bikeshedding if they can’t find anything wrong.
  • 13. Only bad APIs can’t be unit tested. Can’t unit test your API? Too bad, time to refactor it.
  • 14. Build slaves are amazing. Test your code on everything. Virtual machines are cheap, compatibility problems down the line are not.
  • 15. –David Reid, Glyph, et al “Call a function with some arguments and inspect some return values.”
  • 16. How Twisted Works (a short, mostly-correct overview)
  • 17. The Event Loop (Spin me right round, baby, right round)
  • 18. The Reactor • Twisted, being an event-driven framework, provides an event loop, or “reactor”. • This is an infinite loop, wrapped around a blocking call that waits until some I/O is ready • Once the I/O is ready, this fires off an event to run the code that was waiting for it • Runs other tasks while I/O blocked tasks are waiting
  • 19. Using the Reactor • To utilise the reactor, you use Deferreds. • Deferreds are an “IOU” for an actual result sometime in the future • A Deferred contains a callback chain, a series of functions run in sequence after it gets a result • Deferreds get their results from events occurring - such as I/O being received or some task being finished
  • 20. Benefits • For I/O bound applications, allows you to efficiently use CPU resources, as the reactor runs other things in the meantime • Integration with common event loops - for example, GTK or QT, to use your Twisted code into your GTK or QT applications
  • 21. The Callback Chain (Hey, I just met you, and this is crazy,
 but here’s a function, so call it maybe?)
  • 22. The Callback Chain • The easiest way to understand the callback chain is to visualise it as a production line. • A production line is made up of discrete blocks - each doing a separate thing. • Let’s imagine a such a production line…
  • 23. Lets say that we are building a toy car, and we need to do the following: 1. Create toy car body from materials 2. Paint racing stripes on car body 3. Add wheels to the car body 4. Put the completed car in a box
  • 24. Here is how we would do that in psuedo-code Deferreds: ! ! d = Deferred()! !d.addCallback(buildCarBody)! d.addCallback(paintStripes)! d.addCallback(addWheels)! d.addCallback(placeInBox)! # Start the callback chain! d.callback(rawMaterials)
  • 25. Deferreds • Each function in the chain is called with the result of the previous one as its first argument • Functions in the callback chain can return either a result or a Deferred that fires with a result sometime in the future
  • 26. Lists of Deferreds • If you have several Deferred operations to do at the same time, and want your callback to fire only when they are all done, use DeferredList.! • Pass it a list of Deferreds, and it will return a Deferred that fires with a list containing the results of each when they have all finished.
  • 27. Error Handling • Error handling is done by errbacks. • Errbacks are like callbacks, but are run when an exception is raised. • An errback will catch all errors that occur on the chain before it.
  • 28. Deferred Benefits • Forces you to break up your logic into sections, broken up over external interface accesses • Encourages reuse, as these sections quite commonly do one thing to an input and return it • Can be cancelled (eg. if a client disconnected and you don’t care about the result anymore)
  • 29. Deferred Benefits • Discourages global mutable state by having different execution threads share the same global state • No global mutable state means you can scale easier • If your global state is databases, just spin up more Twisted instances to use all your CPUs
  • 30. Extending the Callback Chain (Because sometimes things like to do other things)
  • 31. Deferreds returning Deferreds • Deferreds can contain their own callback chain - meaning that one Deferred returned may have several callbacks being called inside it • This is invisible to the “top level” of callbacks • You can extend your callback chain at any time
  • 32. What can Twisted do? (Enough of the boring stuff!)
  • 33. Web • Klein, a Flask-like API in Twisted • Saratoga, a HTTP API framework • Autobahn, a Websockets implementation • Hendrix, a Django deployment framework • Static file serving: 
 twistd -n web --port tcp:8080 --path /tmp
  • 34. Klein (Flask in Twisted) from klein import Klein! app = Klein()! ! @app.route('/')! def home(request):! return 'Hello, world!'! ! app.run("localhost", 8080)
  • 35. Saratoga (HTTP API) {! "metadata": {! "name": “APIExample", "versions": [1]! },! "endpoints": [{! ! ! "endpoint": “example", "getProcessors": [! {"versions": [1]}! ]}! ]}! from saratoga.api import SaratogaAPI! import json! ! class APIExample(object):! class v1(object):! def example_GET(self, request, params):! return {"hello": "world"}! ! api = SaratogaAPI(APIExample, json.load(open("apidef.json")))! api.run()
  • 36. Email • Complete SMTP and ESMTP server and clients • POP3 and IMAP4 servers and clients • Support for TLS-secured email out of the box
 
 For more info, see:
 http://twistedmatrix.com/documents/ current/mail/index.html
  • 37. DNS • A caching, recursive DNS server out of the box
 twistd -n dns -c —port 10053 --recursive • DNS client APIs • DNS server toolkit
 
 For more info, see:
 http://twistedmatrix.com/documents/ current/names/howto/custom-server.html
  • 38. Chat • IRC Server
 twistd -n words --irc-port tcp:8400 --group roomname --auth memory:usr:pass • IRC client (for making IRC bots) • XMPP client
 
 For more info, see: 
 http://twistedmatrix.com/documents/ current/words/examples/
  • 39. Want to know more? • Twisted Docs
 http://twistedmatrix.com/documents/current/ • Iffy’s Twisted FTW
 http://iffycan.com/twistedftw/ • HawkOwl’s Technicals
 http://technicals.atleastfornow.net/