Twisted (software) explained

Twisted
Logo Size:150px
Author:Glyph Lefkowitz
Developer:Community
Released:[1]
Programming Language:Python
Genre:Event-driven networking
License:MIT License

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 physical 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

Nevow

Nevow
Author:Donovan Preston[13] [14] [15]
Developer:Divmod
Latest Release Version:0.14.4
Latest Release Date:[16]
Programming Language:Python
Operating System:Cross-platform
Genre:Web application framework
License:MIT License

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.[17]

As of mid-2010, Divmod went out of business,[18] causing development work on Nevow to all but cease, and in 2011 its homepage was no longer accessible.[19] There is a project on Launchpad, hosting the source code of Divmod[20] including the source code of the Nevow project.[21]

Athena

Athena is a Nevow component which facilitates bi-directional, asynchronous communication between the Python and JavaScript portions of a web application in the form of remote procedure calls. This technique is typically called Ajax or Comet, though Nevow's implementation predates both of these labels.[22] Athena also includes an inheritance-based JavaScript object system, which forms the basis of a client-side widget abstraction, module system and in-browser unit testing kit.

See also

Notes and References

  1. Shtull-Trauring. Itamar. Twisted 1.0. 2002-10-22. 2008-08-14. twisted-python.
  2. Web site: BuildBot Manual. github.com. 2017-10-28. https://web.archive.org/web/20120729232916/http://djmitche.github.com/buildbot/docs/0.7.10/#Requirements. 2012-07-29. dead.
  3. Web site: Python Slithers into Systems. eweek.com.
  4. [SageMath#Features]
  5. http://sage.math.washington.edu/home/wdj/expository/gap2007/sage-9-2007.pdf Sage a Basic Overview
  6. Web site: mistah deejay. omegler.blogspot.com.
  7. Web site: Google Groups. groups.google.com.
  8. Web site: Calendar and Contacts Server. www.calendarserver.org.
  9. Web site: Twitch - Jobs. Twitch.
  10. Web site: Velocity Weather -. www.velocityweather.com.
  11. Web site: Deluge: build from source. dev.deluge-torrent.org. 2020-01-08.
  12. Web site: Magic Wormhole. github.com.
  13. https://www.python.org/pycon/dc2004/papers/60/context this 2004 paper by Donovan Preston, the original author of Nevow and Formless
  14. http://soundfarmer.com/content/nevow.html Nevow: A Web Application Construction Kit
  15. http://article.gmane.org/gmane.comp.python.twisted/4973/ Subject: Re: ANN: Twisted 1.1.0, the framework of *your* internet
  16. Web site: Releases · twisted/nevow . . 2020-11-04 .
  17. https://wiki.python.org/moin/PythonWebsitePyramidDocs PythonWebsitePyramidDocs
  18. http://glyph.twistedmatrix.com/2009/07/goodbye-divmod-hello-world.html Goodbye, Divmod. Hello, World!
  19. https://www.ohloh.net/p/nevow/commits Divmod Nevow commits
  20. http://divmod.org Divmod
  21. https://launchpad.net/divmod.org Divmod
  22. http://divmodsphinx.funsize.net/nevow/chattutorial/#history History
  23. Web site: EventMachine . 20 August 2011.
  24. Web site: Integrating with other Frameworks — Kivy 1.11.0.dev0 documentation. kivy.org.