Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Why Twisted Is The Best (WOOTConf @ LCA 2017)

Why Twisted Is The Best (WOOTConf @ LCA 2017)

Twisted's pretty good.

Amber Brown (HawkOwl)

January 16, 2017
Tweet

More Decks by Amber Brown (HawkOwl)

Other Decks in Technology

Transcript

  1. Why Twisted Is The Best (and how it will make

    your application awesome) KatieConf/WOOTConf @ linux.conf.au
  2. @hawkieowl "Why Twisted Is The Best" Release Manager for: 13.2,

    14.0, 15.0, 15.1, 15.2, 15.3, 15.4, 15.5, 16.0, 16.1, 16.2, 16.3, 16.4, 16.5, 16.6, 16.7
  3. @hawkieowl "Why Twisted Is The Best" WSGI TCP "Standard" Web

    App Client Client Client Client Client Client
  4. @hawkieowl "Why Twisted Is The Best" 1. Twisted keeps a

    list of connections
 your app is interested in Connection 1 Connection 2 Connection 3 Your App
  5. @hawkieowl "Why Twisted Is The Best" 2. Twisted asks the

    OS which connections
 have data ready, or can take more data select([1, 2, 3]) Connection 1 Connection 2 Connection 3 Your App
  6. @hawkieowl "Why Twisted Is The Best" 3. The OS responds

    with the ones that are. { "readable": [1], "writable: [2, 3] } Connection 1 Connection 2 Connection 3 Your App
  7. @hawkieowl "Why Twisted Is The Best" 4. Twisted recieves data

    and notifies
 your app, which processes the data. dataRecieved("how are you?") Connection 1 Connection 2 Connection 3 Your App
  8. @hawkieowl "Why Twisted Is The Best" 5. Your application can

    send data back on the connection, which Twisted will give the OS. transport.send("good!") socket.send(1, "good!") Connection 1 Connection 2 Connection 3 Your App
  9. @hawkieowl "Why Twisted Is The Best" Factory Listening Socket Protocol

    Instance Transport Connected Socket Protocol Instance Transport Connected Socket Protocol Instance Transport Connected Socket
  10. @hawkieowl "Why Twisted Is The Best" Receives and sends data

    Decodes the data into useful events Handles the connection lifecycle
  11. @hawkieowl "Why Twisted Is The Best" Data is sent to

    a Transport A Transport may get it there in different ways
  12. @hawkieowl "Why Twisted Is The Best" Protocol consumes a full

    HTTP request, tells your application about it
  13. @hawkieowl "Why Twisted Is The Best" HTTP (1.0, 1.1, 2.0)

    SMTP, ESMTP, IMAP, POP3 DNS, SSH, Telnet, TLS ...and a bunch more
  14. @hawkieowl "Why Twisted Is The Best" from klein import Klein

    app = Klein() @app.route("/") def main(self, request): return b"Hello!!!!" app.run('0.0.0.0', 8080)
  15. @hawkieowl "Why Twisted Is The Best" We send a request,

    which Twisted sends immediately... ...but how do we know when we've got all the data back?
  16. @hawkieowl "Why Twisted Is The Best" Twisted Deferred (~2003, Python)

    Dojo Deferred (2008, JavaScript) Promises/A (2009, JavaScript) jQuery Deferred (2011, JavaScript) Promises/A+ (2013, JavaScript)
  17. @hawkieowl "Why Twisted Is The Best" @app.route("/<ip>") async def main(self,

    request, ip): p = await treq.get( "https://google.com") return await p.content() app.run('0.0.0.0', 8080)
  18. @hawkieowl "Why Twisted Is The Best" Transport Operating System Network

    Protocol Business Logic Parsing protocols Abstractions on top TLS (handshakes, certificates) Mostly same interface on Windows, Mac, Linux, BSDs Out-of-the-box logic for some tasks Utilities for coping with network failure/reconnects
  19. @hawkieowl "Why Twisted Is The Best" Transport Operating System Network

    Protocol Business Logic Example: txtorcon A Tor implementation that implements the Protocol and Transport layers Exposes itself to app devs as an Endpoint (which creates Transports)
  20. @hawkieowl "Why Twisted Is The Best" Transport Operating System Network

    Protocol Business Logic Example: qt5reactor A reactor/event loop that interoperates with QT5. Installable and usable by app developers.
  21. @hawkieowl "Why Twisted Is The Best" Twisted allows you to

    listen/connect to as many things as you want*
  22. @hawkieowl "Why Twisted Is The Best" * as long as

    you don't block the reactor with heavy CPU usage
  23. @hawkieowl "Why Twisted Is The Best" Usually you are presented

    with two paths: - the easy way - the performant way
  24. @hawkieowl "Why Twisted Is The Best" All ways of starting

    a Twisted app are equally performant
  25. @hawkieowl "Why Twisted Is The Best" twist, meant for deployment

    task.react, meant for scripts calling reactor.run()
  26. @hawkieowl "Why Twisted Is The Best" Drop-in compatible with Python

    2.7 & 3.5 (soon) Twisted works pretty good!
  27. @hawkieowl "Why Twisted Is The Best" Using Python, you can

    create networking servers that handle thousands of connections at once
  28. @hawkieowl "Why Twisted Is The Best" Speak any protocol you

    want, talk to basically anything that exists
  29. @hawkieowl "Why Twisted Is The Best" You can write hybrid

    networking applications super easily!
  30. @hawkieowl "Why Twisted Is The Best" High-level enough to get

    things done, low-level enough that you can make it do exactly as you want
  31. @hawkieowl "Why Twisted Is The Best" It's all Python! Write

    Python, use Twisted whenever you need to talk to the network!
  32. @hawkieowl "Why Twisted Is The Best" No configuration Opinionated in

    security Unopinionated in how you build your application