ArticleslgStudy

computer science

Twisted (software)

Twisted (software) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Twisted (software) rather than just read about it. In short: Twisted is an event-driven network programming framework written in Python and licensed under the MIT License. Twisted projects variously support TCP, UDP, SSL/TLS, IP multicast, Unix domain sockets, many protocols (including HTTP, XMPP, NNTP, IMAP, SSH, IRC, FTP, and others), and much more.

Twisted (software) — main illustration
Twisted (software) — illustration

Key takeaways

  • Twisted (software) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Twisted (software) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Twisted (software) from memory before moving on to harder problems.

Reference excerpt

Twisted is an event-driven network programming framework written in Python and licensed under the MIT License. Twisted projects variously support TCP, UDP, SSL/TLS, IP multicast, Unix domain sockets, many protocols (including HTTP, XMPP, NNTP, IMAP, SSH, IRC, FTP, and others), and much more. Twisted is based on the event-driven programming paradigm, which means that users of Twisted write short callbacks which are called by the framework.

Core ideas

Separation of protocols and transports Twisted is designed for complete separation between logical protocols (usually relying on stream-based connection semantics, such as HTTP or POP3) and transport layers supporting such stream-based semantics (such as files, sockets or SSL libraries). Connection between a logical protocol and a transport layer happens at the last possible moment — just before information is passed into the logical protocol instance. The logical protocol is informed of the transport layer instance, and can use it to send messages back and to check for the peer's identity. Note that it is still possible, in protocol code, to deeply query the transport layer on transport issues (such as checking a client-side SSL certificate). Naturally, such protocol code will fail (raise an exception) if the transport layer does not support such semantics.

Deferreds Central to the Twisted application model is the concept of a deferred (elsewhere called a future). A deferred is an instance of a class designed to receive and process a result which has not been computed yet, for example because it is based on data from a remote peer. Deferreds can be passed around, just like regular objects, but cannot be asked for their value. Each deferred supports a callback chain. When the deferred gets the value, it is passed to the functions on the callback chain, with the result of each callback becoming the input for the next. Deferreds make it possible to operate on the result of a function call before its value has become available. For example, if a deferred returns a string from a remote peer containing an IP address in quad format, a callback can be attached to translate it into a 32-bit number. Any user of the deferred can now treat it as a deferred returning a 32-bit number. This, and the related ability to define "errbacks" (callbacks which are called as error handlers), allows code to specify in advance what to do when an asynchronous event occurs, without stopping to wait for the event. In non-event-driven systems, for example using threads, the operating system incurs premature and additional overhead organizing threads each time a blocking call is made.

Thread support Twisted supports an abstraction over raw threads — using a thread as a deferred source. Thus, a deferred is returned immediately, which will receive a value when the thread finishes. Callbacks can be attached which will run in the main thread, thus alleviating the need for complex locking solutions. A prime example of such usage, which comes from Twisted's support libraries, is using this model to call into databases. The database call itself happens on a foreign thread, but the analysis of the result happens in the main thread.

Foreign loop support Twisted can integrate with foreign event loops, such as those of GTK+, Qt and Cocoa (through PyObjC). This allows using Twisted as the network layer in graphical user interface (GUI) programs, using all of its libraries without adding a thread-per-socket overhead, as using Python's native library would. A full-fledged web server can be integrated in-process with a GUI program using this model, for example.

Applications using Twisted

The BuildBot continuous-integration system relies on Twisted for client/server communication. ITA Software has developed an airline-reservation system for Air Canada that uses Twisted extensively. SageMath, an open-source alternative to Mathematica, Maple, Magma, MATLAB, has a web-based interface, SageMath notebook, that runs on a Twisted server. Twisted was used in the Omegle one-on-one chat service until it was replaced with gevent for performance reasons. The Apple Calendar Server uses Twisted, as do some internal projects of NASA. Conch, an implementation of the Secure Shell (SSH) protocol The original version of social networking and microblogging site Jaiku used Twisted. Fluidinfo, an online cloud data-store, uses Twisted extensively for internal RPC (partly in combination with Thrift and AMQP), for its internal services, and for external APIs. The file-hosting service Ubuntu One used Twisted. Tor2web, an HTTP proxy for Tor Hidden Services (HS), uses Twisted. GlobaLeaks, an open-source whistleblowing framework, uses Twisted. Cloudkick, a cloud-server management web-application, used Twisted. It now has been rewritten using Node.js. Twilio, a cloud telephony provider, uses Twisted. Twitch, a video game broadcasting and chat community, uses Twisted. Velocity Weather, a meteorological data processing and integration API is built on Twisted. qwebirc, a web-based IRC client, uses Twisted. Zenoss Core, a network management platform, uses Twisted for many internal and collection daemons. Scrapy, a web crawler based on Twisted. Listen to Wikipedia, a Wikipedia audio-visualizer, uses Twisted to broadcast real-time edit events to browsers. Tahoe-LAFS, a distributed data store and distributed file system. Deluge, a highly modular BitTorrent client, uses Twisted. Magic Wormhole, a secure file transfer tool using PAKE.

Nevow

Nevow (pronounced like the French nouveau) is a Python web application framework originally developed by the company Divmod. Template substitution is achieved via a small Tag Attribute Language, which is usually embedded in on-disk XML templates, though there is also a pure-Python domain-specific language called Stan, for expressing this markup programmatically. Nevow integrates well with Twisted. Nevow was deployed on several high-profile web sites, most notably the official Python site. As of mid-2010, Divmod went out of business, causing development work on Nevow to all but cease, and in 2011 its homepage was no longer accessible. There is a project on Launchpad, hosting the source code of Divmod including the source code of the Nevow project.

… excerpt ends here. Continue reading the full article.

Illustrations

Twisted (software) illustration

Worked examples

Example 1 — a first encounter with Twisted (software)

Start with the simplest possible case. Write down what Twisted (software) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Twisted (software) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Twisted (software) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Twisted (software)

In research
Twisted (software) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Twisted (software) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Twisted (software) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Free network-related software, Free software programmed in Python, Free system software, so understanding it makes those chapters shorter.
In everyday life
Look for Twisted (software) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Twisted (software)” →

How to study Twisted (software) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Twisted (software) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Twisted (software) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Twisted (software) in simple terms?

Twisted is an event-driven network programming framework written in Python and licensed under the MIT License. Twisted projects variously support TCP, UDP, SSL/TLS, IP multicast, Unix domain sockets, many protocols (including HTTP, XMPP, NNTP, IMAP, SSH, IRC, FTP, and others), and much more.

Why does Twisted (software) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Twisted (software)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Twisted (software).

Tags

  • Free network-related software
  • Free software programmed in Python
  • Free system software
  • Python (programming language) libraries
  • Software using the MIT license

Keep exploring