Why Twisted Is The Best (WOOTConf @ LCA 2017)

Why Twisted Is The Best (WOOTConf @ LCA 2017)

Twisted's pretty good.

3d37232726396a1d3c7412dd915095ea?s=128

Amber Brown (HawkOwl)

January 16, 2017
Tweet

Transcript

  1. 1.

    Why Twisted Is The Best (and how it will make

    your application awesome) KatieConf/WOOTConf @ linux.conf.au
  2. 5.
  3. 6.
  4. 7.

    @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
  5. 10.

    @hawkieowl "Why Twisted Is The Best" WSGI TCP "Standard" Web

    App Client Client Client Client Client Client
  6. 14.

    @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
  7. 15.

    @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
  8. 16.

    @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
  9. 17.

    @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
  10. 18.

    @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
  11. 20.
  12. 24.
  13. 27.

    @hawkieowl "Why Twisted Is The Best" Factory Listening Socket Protocol

    Instance Transport Connected Socket Protocol Instance Transport Connected Socket Protocol Instance Transport Connected Socket
  14. 28.

    @hawkieowl "Why Twisted Is The Best" Receives and sends data

    Decodes the data into useful events Handles the connection lifecycle
  15. 30.

    @hawkieowl "Why Twisted Is The Best" Data is sent to

    a Transport A Transport may get it there in different ways
  16. 31.
  17. 33.

    @hawkieowl "Why Twisted Is The Best" Protocol consumes a full

    HTTP request, tells your application about it
  18. 36.

    @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
  19. 40.

    @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)
  20. 44.

    @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?
  21. 47.
  22. 48.

    @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)
  23. 49.

    @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)
  24. 53.

    @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
  25. 55.

    @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)
  26. 56.

    @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.
  27. 62.

    @hawkieowl "Why Twisted Is The Best" Twisted allows you to

    listen/connect to as many things as you want*
  28. 63.

    @hawkieowl "Why Twisted Is The Best" * as long as

    you don't block the reactor with heavy CPU usage
  29. 66.
  30. 72.
  31. 74.

    @hawkieowl "Why Twisted Is The Best" Usually you are presented

    with two paths: - the easy way - the performant way
  32. 76.

    @hawkieowl "Why Twisted Is The Best" All ways of starting

    a Twisted app are equally performant
  33. 77.

    @hawkieowl "Why Twisted Is The Best" twist, meant for deployment

    task.react, meant for scripts calling reactor.run()
  34. 80.

    @hawkieowl "Why Twisted Is The Best" Drop-in compatible with Python

    2.7 & 3.5 (soon) Twisted works pretty good!
  35. 82.

    @hawkieowl "Why Twisted Is The Best" Using Python, you can

    create networking servers that handle thousands of connections at once
  36. 83.

    @hawkieowl "Why Twisted Is The Best" Speak any protocol you

    want, talk to basically anything that exists
  37. 84.

    @hawkieowl "Why Twisted Is The Best" You can write hybrid

    networking applications super easily!
  38. 85.

    @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
  39. 86.

    @hawkieowl "Why Twisted Is The Best" It's all Python! Write

    Python, use Twisted whenever you need to talk to the network!
  40. 87.

    @hawkieowl "Why Twisted Is The Best" No configuration Opinionated in

    security Unopinionated in how you build your application