Introduction
Twisted is one of the oldest and most comprehensive async networking frameworks in the Python ecosystem. Started in 2002, it provides protocol implementations for dozens of network protocols and a battle-tested event loop that predates Python's built-in asyncio by over a decade.
What Twisted Does
- Implements clients and servers for HTTP, SMTP, IMAP, SSH, DNS, IRC, and more
- Runs an event-driven reactor that handles thousands of concurrent connections
- Provides Deferred objects for composable asynchronous callback chains
- Includes a test framework (Trial) specifically designed for testing async code
- Supports TLS/SSL natively with certificate verification and SNI
Architecture Overview
Twisted is built around a reactor pattern. The reactor is a central event loop that monitors file descriptors, timers, and signals, dispatching events to protocol handlers. Protocols are layered on top of transports, cleanly separating parsing logic from I/O mechanics. The Deferred abstraction provides chainable callbacks and error handlers, similar to Promises in JavaScript. Modern Twisted also supports async/await via ensureDeferred and integrates with asyncio's event loop.
Self-Hosting & Configuration
- Install with
pip install twistedor specific extras liketwisted[tls] - Use
twistdto run Twisted applications as daemons with logging and PID management - Write
.tacapplication files to define long-running services - Enable TLS with
twisted.internet.ssland provide certificate chain files - Deploy behind a reverse proxy for HTTP applications or run standalone for protocol servers
Key Features
- Protocol implementations for over 20 network protocols in a single library
- The Deferred system provides structured error propagation across async chains
- Trial test runner handles async test cases with reactor cleanup between tests
- Conch module provides a full SSH client and server implementation in pure Python
- Incremental module provides composable, streaming data parsers
Comparison with Similar Tools
- asyncio — stdlib event loop with fewer built-in protocols, newer API
- Tornado — async web framework focused on HTTP, narrower protocol support
- gevent — monkey-patches blocking I/O, different concurrency model
- Trio — modern structured concurrency library, less protocol coverage
- aiohttp — HTTP client/server for asyncio, no multi-protocol support
FAQ
Q: Should I use Twisted or asyncio for new projects? A: For simple HTTP or WebSocket services, asyncio with a framework like FastAPI is often simpler. Twisted remains the better choice when you need implementations of non-HTTP protocols (SSH, SMTP, DNS) or need the mature Deferred-based error handling.
Q: Can Twisted work with asyncio?
A: Yes. Twisted can run on the asyncio event loop via asyncioreactor, and ensureDeferred lets you use async/await syntax within Twisted code. The two ecosystems interoperate well.
Q: Is Twisted still maintained? A: Yes. Twisted has an active maintainer team and regular releases. It is used in production by large organizations for protocol servers, IoT gateways, and network tooling.
Q: What is a Deferred? A: A Deferred is Twisted's equivalent of a Promise. It represents a value that will be available in the future, with methods to attach success callbacks and error handlers in a chain.